Code Samples /

Ordering by Meta Query

Ajax Load More supports a single orderby parameter, however, the WP_Query class allows for extending this by ordering by a specific Meta Query.

The code below uses the alm_query_args filter to append an ever-present Meta Query to the query arguments and then order the results by the clause.

functions.php
<?php 
function my_movie_listing( $args, $id ){ 
   // [ajax_load_more id="movie_listing"]

   // Custom meta query clause.
   $date_args = [ 
      'key'     => 'my_date_field',
      'value'   => date( "Ymd" ),
      'compare' => '>=' ,
      'type'    => 'DATETIME' 
   ];

   // Add to meta_query.
   $args['meta_query']['date_clause'] = $date_args; 

   // Add custom ordering.
   $args['orderby'] = [
      'date_clause' => 'ASC',
   ];

   return $args; 
}
add_filter( 'alm_query_args_movie_listing', 'my_movie_listing', 10, 2 );
JavaScript

« Back to Code Samples