Order custom post type by date field
-
I have a custom post type that is simply titles with a date field. I want to display these posts in a list in order of this date field (not date posted). So simply:
—
Sample date
Event title 1
Event title 2Sample date
Event title 3
Event title 4
Event title 5(etc)
—
No extra info, that’s literally all I want to display.
I can’t figure out how to go about this at all. I’m not great at PHP so I’d appreciate any help with this.
-
you need to order by meta_value. see below
query_posts( array(
‘post_type’ => ‘services’,
‘order’ => ‘ASC’,
‘orderby’ => ‘meta_value’,
‘meta_key’ => ‘date-field’
) );Thanks for the response.
So where you’ve put ‘date-field’ I need to include the custom field name? I’ve tried this but nothing is showing.
Here’s the code I’m using for the custom post type, it’s using the my-meta-box plugin for the date field (seen here) so I’m unsure how to call this as you have above:
//register your custom post type match add_action( 'init', 'register_cpt_match' ); function register_cpt_match() { $labels = array( 'name' => _x( 'Matches', 'Match' ), 'singular_name' => _x( 'match', 'match' ), 'add_new' => _x( 'Add New', 'match' ), 'add_new_item' => _x( 'Add New Match', 'match' ), 'edit_item' => _x( 'Edit match', 'match' ), 'new_item' => _x( 'New match', 'match' ), 'view_item' => _x( 'View match', 'match' ), 'search_items' => _x( 'Search match', 'match' ), 'not_found' => _x( 'No Matches found', 'match' ), 'not_found_in_trash' => _x( 'No Matches found in Trash', 'match' ), 'parent_item_colon' => _x( 'Parent match:', 'match' ), 'menu_name' => _x( 'Matches', 'match' ), ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'description' => 'just a simple "where, when, with who" kind of thing', 'supports' => array( 'title', 'custom-fields' ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_icon' => 'https://i.imgur.com/4nTMD.png', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post' ); register_post_type( 'match', $args ); } if (is_admin()){ //include the main class file require_once("/home/urlblahblah.php"); $prefix = '_Matches'; //configure your meta box $config = array( 'id' => 'Matches-info', 'title' => 'Match Info', 'pages' => array('match'), 'context' => 'normal', 'priority' => 'high', 'fields' => array(), 'local_images' => false, 'use_with_theme' => false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false). ); $my_meta = new AT_Meta_Box($config); //Add fields to your meta box $my_meta->addDate($prefix.'when',array('name'=> 'When is the match ')); $my_meta->Finish(); }
*naturally the url in the code above isn’t actually ‘/home/urlblahblah.php’, just changed it for security reasons.
is this for backend post listing or front end?
Front end. Just a simple event list with dates and titles. Am I going about it the wrong way?
I’ve tried to pull the posts with a query, using your code above and replacing ‘date-field’ with ‘when’ as I think that’s the custom field name as far as I can tell. But no dice. (It shows the posts without this by the way, but just ordered normally, by post date.)
If its front end this should work
query_posts( array( 'post_type' => 'match', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'date-field' ) ); while ( have_posts() ) : the_post(); echo ' <li>'; the_title(); echo '</li> '; endwhile;
Also in your code I don’t see any date fields in meta box
I presumed this was where the date field was being added in my backend code:
//Add fields to your meta box $my_meta->addDate($prefix.'when',array('name'=> 'When is the match ')); $my_meta->Finish();
I’ve a feeling I’m making this far more complicated than it ought to be.
https://designhammer.com/blog/sorting-events-date-using-wordpress-custom-post-types
this is what you need.
Note: instead of saving dates in date format save it as string like in above tutorial. After that this should work;
query_posts( array(
‘post_type’ => ‘match’,
‘order’ => ‘ASC’,
‘orderby’ => ‘meta_value’,
‘meta_key’ => ‘when’
) );Thanks a lot for your help.
I’ve managed to get my code working, so that it orders the posts by the custom date field. But it’s displaying the date for every title, even if they’re on the same date. I need to find a way to group events on the same day under one date. Hopefully this isn’t much more complex.
see the code below
<?php $the_query = new WP_Query( array( 'post_type' => 'match', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'when' ) ); $current_header = ''; while ( $the_query->have_posts() ) : $the_query->the_post(); # get the datum for this post $temp_date = get_post_meta( get_the_ID(), 'when', true ); # If they aren't the same, we'll start a new group, which for now # just means setting a new heading if ( $temp_date != $current_header ) { $current_header = $temp_date; echo "<h2>$current_header</h2>"; } the_title(); # ... do normal loop stuff here endwhile; wp_reset_query(); ?>
Perfect! Thank you.
Is there any way to have it so it only displays posts after the current date?
Figured out how to have it only display posts after the current time. Thanks for your help, I really appreciate it!
Final code:
<dl id="sportlist"> <?php $the_query = new WP_Query( array( 'post_type' => 'match', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'date_field' ) ); $current_header = ''; while ( $the_query->have_posts() ) : $the_query->the_post(); # get the datum for this post $temp_date = get_post_meta( get_the_ID(), 'date_field', true ); # If they aren't the same, we'll start a new group, which for now # just means setting a new heading if (strtotime($temp_date) > strtotime('now')){ if ( $temp_date != $current_header ) { $current_header = $temp_date; echo "<dt>$current_header</dt>"; }?> <dd><?php the_title();?></dd> <?php } endwhile; wp_reset_query();?> </dl>
- The topic ‘Order custom post type by date field’ is closed to new replies.