Order By Meta Values Using WP_Query
With WordPress you can use multiple ways to get a list of posts or content items, some of them will allow you to get the default posts and pages so you can loop through them and display these in your theme. The other option is to create a custom query to get specific posts from your content.
To create custom queries the best option is to use the WP_Query class. The WP_Query class is defined in wp-includes/query.php this class is used to make queries to the database which get the posts we need for a certain page. The WP_Query class will create a variable which can be used anywhere on the page, the variable is $wp_query and has a number of methods which can be used to get information from the query.
The main methods that you may already be aware of is the $wp_query->have_posts() method which is called from within the loop by the have_posts() function. You can also use this variable inside The Loop by getting the information for the current post by using the the_post() method.
If you are using the WP_Query class to modify the WordPress query then you need to use the variable of the class to call the have_posts() methods and the the_post() method.
$new_query = new WP_Query( $args ); // The Loop while ( $new_query->have_posts() ) : $new_query->the_post(); printf('<h1>%s</h1>', get_the_title() ); endwhile;
Using the WP_Query class you can query posts which have a certain meta key and meta value. For example if you have an events post type and one of your meta fields is the event start date then you will want to make sure you only get post types which have this meta key set. You will need to query all posts with this meta value by using the parameter meta_query. The meta_query parameter allows you to provide and key, value and compare type to make sure you return the correct posts.
$args = array( 'post_type' => 'event', 'meta_query' => array( array( 'key' => 'start_date', 'value' => date('Ymd'), 'compare' => '>=' ) ) ); $query = new WP_Query( $args );
With the above query you will return all events which have a start date value which is in the future, but the posts will not be returned in order of this start date but will be in order of published date, so if you now publish a new event that starts tomorrow it will return at the bottom of the list. To return the events with the next event at the top of the list you need to change the wp_query object and add a orderby parameter, with a value of meta_value.
$args = array( 'post_type' => 'event', 'orderby' => 'meta_value', 'meta_query' => array( array( 'key' => 'start_date', 'value' => date('Ymd'), 'compare' => '>=' ) ) ); $query = new WP_Query( $args );
When we want to order by the meta value we also need to define the meta key and compare by supplying the same information inside the meta_query.
$args = array( 'post_type' => 'event', 'order' => 'DESC', 'orderby' => 'meta_value', 'meta_key' => 'start_date', 'meta_query' => array( array( 'key' => 'start_date', 'value' => date('Ymd'), 'compare' => '>=' ) ) ); $query = new WP_Query( $args );
This will then return the posts with a start date in order of the next event at the top of the list, allowing you to use this in a latest events page for your website.
Subscribe To Newsletter
Get weekly updates to your email