Sort posts in a widget by 2 custom fields?

[ad_1]

\[Editing to add: I spent quite a bit of time this morning with ChatGPT and failed to come up with a solution. So please don’t suggest that approach.\]

I have 2 Advanced Custom Fields for standard posts: start\_date (Date picker) and start\_time (Time picker). Both are optional, and start\_time is only available if there is a value for start\_date.

I have a custom widget that only shows posts that have a start\_date value, and it successfully shows only posts where the start\_date is today or later, and not more than 15 days from today. The posts are sorted by start\_date in ascending order.

All that works just fine.

BUT (because there’s always a “but”, isn’t there?) I can’t figure out how to sort on the start\_time if it exists.

February 23
February 23, 10:00am
February 23, 2:00pm
March 1
March 2, 8:00pm

And so on.

This is the $args array:

$args = array(
‘ignore_sticky_posts’=> 0,
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
‘meta_query’ => array(
array(
‘key’ => ‘start_date’, // ACF field for event start date
‘value’ => date(‘Ymd’, strtotime(“+{$nr_days_ahead} days”)), // restrict start date to no more than today + $nr_days_ahead
‘compare’ => ‘<=’,
‘type’ => ‘DATE’
),
array(
‘key’ => ‘start_date’, // ACF field for event start date
‘value’ => $today,
‘compare’ => ‘>=’,
‘type’ => ‘DATE’
)
),
‘posts_per_page’ => $nr_posts,
‘cat’ => $categories,
‘meta_key’ => ‘start_date’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘ASC’
);

Suggestions?

[ad_2]
1 Comment
  1. Haha, let me ChatGPT that for you:

    You need to modify the $args array to include another meta_query for start_time. Here is the modified $args array:

    $args = array(
    ‘ignore_sticky_posts’ => 0,
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘start_date’, // ACF field for event start date
    ‘value’ => date(‘Ymd’, strtotime(“+{$nr_days_ahead} days”)), // restrict start date to no more than today + $nr_days_ahead
    ‘compare’ => ‘<=’,
    ‘type’ => ‘DATE’
    ),
    array(
    ‘key’ => ‘start_date’, // ACF field for event start date
    ‘value’ => $today,
    ‘compare’ => ‘>=’,
    ‘type’ => ‘DATE’
    ),
    array(
    ‘key’ => ‘start_time’, // ACF field for event start time
    ‘compare’ => ‘EXISTS’ // only include posts that have a start_time value
    )
    ),
    ‘posts_per_page’ => $nr_posts,
    ‘cat’ => $categories,
    ‘orderby’ => array(
    ‘meta_value_num’ => ‘ASC’, // sort by start_date in ascending order
    ‘meta_value’ => ‘ASC’ // then sort by start_time in ascending order
    ),
    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘start_date’,
    ‘compare’ => ‘NOT EXISTS’ // include posts that do not have a start_time value
    ),
    array(
    ‘key’ => ‘start_time’,
    ‘compare’ => ‘EXISTS’ // only include posts that have a start_time value
    )
    )
    );

    In this modified $args array, new meta_query for start_time is added, which checks whether the field exists or not. The orderby parameter to sort by both start_date and start_time in ascending order.
    Finally, we added a new meta_query to include posts that do not have a start_time value. This is necessary because otherwise, posts without a start_time value would be excluded from the results. Note that the code assumes that the start_time is in the format “H:i” (e.g. “10:00”). If your start_time format is different, you may need to adjust the code accordingly.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer