• Hi

    I wondered if someone could help me with required code to sort my posts by custom field. The custom field is an event date, with the earliest showing first. I know a lot of people have asked this but there isnt quite enough info out there. Any info would be gratefully received.

    The custom field is called ‘event_date’.

    Thanks

Viewing 15 replies - 1 through 15 (of 15 total)
  • I have the code, I’ll post as soon as i can dig it up unless someone beats me.

    Didn’t test but I know I used it before and it worked.

    <ul>
    <?php
    query_posts('meta_key=your key&meta_value=your value');
    ?>
    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_title();?>
        <?php endwhile; ?>
         <?php endif; ?>
        <?php wp_reset_query();?>
     </ul>

    Adapt as needed.

    Thread Starter rob_870

    (@rob_870)

    ok thanks, just to check
    meta key would = ‘event_date’ but what would the ‘meta value’ be?

    Also, this goes before the loop on index.php and page template php?

    Cheers
    Rob

    key is what you named the field, the value is what you enter in the field.
    Pretty sure you can use it before or after the loop, or in a sidebar. I used it in a php widget as well as in a page template.
    I also used it in within the meta div of single.php, so the feilds would appear with other meta.

    Thread Starter rob_870

    (@rob_870)

    right, ok my values are all different dates so what would I enter here? ‘yyyy-mm-dd’?

    For example
    (‘meta_key=event_date&meta_value=yyyy-mm-dd’)

    Thanks again

    You have to enter the same value as you put in the field. It will get all posts with that value. So I assume one of your values is a date like “2012, Febuary, 14” ..so that would be wht you put in the code.

    I think if you query the id it will show all posts with any date given as a value for that id.

    Thread Starter rob_870

    (@rob_870)

    oh right ok I thought so. Sorry perhaps I didnt explain it properly. It would have to order posts by the event date with soonest first. As they all have different dates it wouldnt be possible to input a different meta value if you see what i mean

    Thanks

    I quickly found this, if you dig around you can figure out how to do it. Supposedly this queries by numeric value, although i see it has 2 keys. There’s more to this than I understand but the info is out there if you dig.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts(array(
    'post_type' => 'event',
    'meta_key' => 'end_time',
    'meta_compare' =>'>=',
    'meta_value'=>time(),
    'meta_key' => 'start_time',
    'orderby' => 'meta_value_num',
    'post_per_page' => '-1',
    'order' => 'ASC',
    'paged' => $paged
    ));

    This also has paging.

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    this is a mess… but this is how i got it to work for me.

    where category is “shows”
    custom field date is “show_info_date”
    and artist is “san-gabriel”

    ?

    Nice! post the code again if you get a chance, use pastebin so it doesn’t get dumped.

    uh.. like this ? haven’t used that before. is java or iframe better for this instance ?

    <script src="https://pastebin.com/embed_js.php?i=zewGPhxe"></script>

    where category is “shows”
    custom field date is “show_info_date”
    and artist is “san-gabriel” (changed to be wp_title since)

    hmm… i’ll have to go read how to use paste bin properly…

    it’s a good tool. Just post a link to your pastebin code, use the link code button.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Just paste the URL and don’t try to use the embedded script. As you’ve found out, it won’t work here.

    https://pastebin.com/zewGPhxe

    thanks !

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Order posts by custom field’ is closed to new replies.