WordPress.org

Ready to get started?Download WordPress

Forums

Order posts by custom field (16 posts)

  1. rob_870
    Member
    Posted 2 years ago #

    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

  2. deepbevel
    Member
    Posted 2 years ago #

    I have the code, I'll post as soon as i can dig it up unless someone beats me.

  3. deepbevel
    Member
    Posted 2 years ago #

    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.

  4. rob_870
    Member
    Posted 2 years ago #

    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

  5. deepbevel
    Member
    Posted 2 years ago #

    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.

  6. rob_870
    Member
    Posted 2 years ago #

    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

  7. deepbevel
    Member
    Posted 2 years ago #

    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.

  8. rob_870
    Member
    Posted 2 years ago #

    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

  9. deepbevel
    Member
    Posted 2 years ago #

    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.

  10. deepbevel
    Member
    Posted 2 years ago #

  11. sheriffderek
    Member
    Posted 2 years ago #

    [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"

    ?

  12. deepbevel
    Member
    Posted 2 years ago #

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

  13. sheriffderek
    Member
    Posted 2 years ago #

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

    <script src="http://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...

  14. deepbevel
    Member
    Posted 2 years ago #

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

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

    http://pastebin.com/zewGPhxe

  16. sheriffderek
    Member
    Posted 2 years ago #

    thanks !

Topic Closed

This topic has been closed to new replies.

About this Topic