0

I am building a movie archive. For each movie I am filling out the release dates for 3 countries, they're stored as custom fields (added via Pods)

Now, when listing the movies I would like to build an array and loop through each post to check if the fields have values, then compare the values and sort by the first date (oldest or least recent date). If none of the fields have values the posted date should be used for sorting. I am already sorting posts of some categories and their child categories by one custom field like this:

function orderby_additionaldate($query) {
if (! is_admin() && $query->is_main_query() && is_category( array( 2,57,530 ) ) || cat_is_ancestor_of(2, get_query_var('cat') ) && $query->is_main_query() && ! is_admin() || cat_is_ancestor_of(57, get_query_var('cat') ) && $query->is_main_query() && ! is_admin() || cat_is_ancestor_of(530, get_query_var('cat') ) && $query->is_main_query() && ! is_admin() ) {
if ( $query->query_vars ) {
    $query->set( 'order', 'DESC' );
    $query->set( 'meta_key', 'additional_date' );
    $query->set( 'orderby', 'meta_value' );
}
}
}
add_action( 'pre_get_posts', 'orderby_additionaldate' );

2 things are missing from this:

  • It's not comparing 3 meta key values (release_f, release_uk and release_us), and
  • It doesn't check if one of them is empty and use the posted date instead (it just sets the value of the empty ones to Jan 1 1970)

I would like to create a similar function to the one above for

if (is_post_type_archive('project') )

then it should check the 3 meta_keys release_f, release_uk and release_us for values, if there are any pick the oldest date, if there is no value at all, pick the post's date and then orderby the result of this Array. Is this possible?

  • If you want to order by an undefined value, you will need to calculate and save it first. Each row can not be sorted by different column/data. So when posts are created, you need to save the value what will be used for sorting. use save_post hook, and save the appropriate (either 3 meta value or post date) value into another meta key. Then sort by this meta field. – Shazzad Feb 8 '14 at 2:39
1

While the query you describe is not impossible in SQL, it is not going to be very efficient as the query would have to evaluate every row in the table matching those keys, and then sort based on the result of the calculation.

You are better saving a fourth value and sorting on that, as suggested by @Shazzad. Something like this:

    function gen_sort_meta_wpse_133851($pid) {
      var_dump($pid);
      $release[] = get_post_meta($pid,'release_f',true);
      $release[] = get_post_meta($pid,'release_uk',true);
      $release[] = get_post_meta($pid,'release_us',true);
      $sort = max($release);
      $release_sort = get_post_meta($pid,'_release_sort',true);
      update_post_meta($pid,'_release_sort',$sort,$release_sort);
    }
    add_action('save_post','gen_sort_meta_wpse_133851');

You should then be able to sort with:

function orderby_additionaldate($query) {
  if (! is_admin() 
    && $query->is_main_query() 
    && is_category( array( 2,57,530 ) ) 
    || cat_is_ancestor_of(2, get_query_var('cat') ) 
    && $query->is_main_query() 
    && ! is_admin() 
    || cat_is_ancestor_of(57, get_query_var('cat') ) 
    && $query->is_main_query() 
    && ! is_admin() 
    || cat_is_ancestor_of(530, get_query_var('cat') ) 
    && $query->is_main_query() 
    && ! is_admin() 
  ) {
    if ( $query->query_vars ) {
        $query->set( 'order', 'DESC' );
        $query->set( 'meta_key', '_release_sort' );
        $query->set( 'orderby', 'meta_value' );
    }
  }
}
add_action( 'pre_get_posts', 'orderby_additionaldate' );

I have no idea if that logic is correct. Long lists of unparenthesized boolean conditions is a good way to get unexpected behavior.

  • thanks for that! I decided to take the easy way and add a 4th custom field and fill the date in manually. Question: Why does cat_is_ancestor_of(530, get_query_var('cat') ) not work when I use permalinks? – Mia Feb 18 '14 at 19:47
  • ok, got it to work with pretty permalinks when I finally found this: wordpress.stackexchange.com/questions/46008/… – Mia Feb 20 '14 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.