Home > Software engineering >  Wordpress post grid sort by custom field only when it has value
Wordpress post grid sort by custom field only when it has value

Time:11-13

I have a custom field for posts named "sort by date". I want to be able to sort my custom grid by post create date unless this custom "sort by date" field has a value... in other words, if the custom field has a value I want wordpress to take that date and leverage it for sorting purposes.

Right now I have my variables defined as

$fields['customDate'] = get_field('updated_date', $post->ID);
    
    if (!empty($fields['customDate'])) {
            $fields['orderDate'] = $fields['customDate'];
        } else {
        $fields['orderDate'] = get_the_date('Ymd', $post);
    }

For displaying the dates on the page this works fine <?php echo date('F j, Y', strtotime($fields['orderDate']));?>

For my query I have

$posts = get_posts(array(
            'post_type' => 'post',
            'numberposts' => $numberposts,
            'tax_query' => $taxQueries,
            'post__not_in' => $exclusions,
            
            'orderby' => array(
                'orderDate' => 'DESC'
            )
            ));

However, I do realize that by not having values in my custom field for all other posts in the database this isn't going to work.

Is there a way to sort by combining the two fields (create date and the custom field) so that if the custom field has value use it for sorting, if not use the post create date?

Thanks in advance!

CodePudding user response:

You'll need to grab the posts then sort them on your own and then feed that to the query via the post__in parameter

Define the sorting function

function my_sort($a,$b){
    $t1 = strtotime($a['date']);
    $t2 = strtotime($b['date']);
    return $t1 - $t2;
}

Get all post IDs

$post_ids = get_posts(array(
    'fields' => 'ids',
    'post_type' => 'post',
    'numberposts' => $numberposts,
    'tax_query' => $taxQueries,
    'post__not_in' => $exclusions,
));

Set the array for later use

$get_posts = $post_ids = array();

Loop through the ids and check if the meta value is set and store in new array

foreach($post_ids as $id){
    $updated_date = get_field('updated_date', $id);
    $date = $updated_date ? $updated_date : get_the_date('Ymd', $id);
    $get_posts[] = array('id' => $id, 'date' => $date );
}

Sort the new post array

usort($get_posts, 'my_sort');

Loop through the sorted array and get the ids

foreach($get_posts as $p ){
    $post_ids[] = $p['id'];
}

And finally get the posts from the sorted array ordered by our sort

$posts = get_post(array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'post__in'  => $post_ids,
    'order' => 'DESC',
    'orderby'=> 'post__in',
));
  • Related