this is probably easy but I can't quite get it sorted. I have an ACF datetime field that I'm using for public notices called "relevant_date." This field exists because the homepage features a small div with the most timely, relevant-to-the-moment public notice, and I can't sort them by the normal post date because the latest post might not be the one closest to the present moment. I hope that makes sense.
Any help appreciated. Thanks!
Here's what I tried:
add_action( 'elementor/query/homepage_public_notices', function( $query ) {
$today = date_i18n ('Y-m-d');
$query->set( 'meta_query', array(
array(
'key' => 'relevant_datetime',
'compare' => '=',
'value' => $today,
'type' => 'DATE',
)
) );
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC');
$query->set('meta_key', 'relevant_datetime');
} );
Let's analyze and debug problem:
-Are you in a good action hook? I can't find it online.
-If you are in the good hook, are you getting good results without sorting?
-If you have results, then all that remains is sorting.
add_action( 'elementor/query/homepage_public_notices', function( $query ) {
$today = date_i18n ('Y-m-d');
$meta_key = 'relevant_datetime';
$query->set( 'meta_query', array(
array(
'key' => $meta_key,
'compare' => '=',
'value' => $today,
'type' => 'DATE',
)
) );
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC');
$query->set('meta_key', $meta_key);
} );
As you have shown, it should work if all conditions are OK. But I would also try the following parameters:
// Retrieves posts by Post Status 'publish'
$query->set('post_status', 'publish');
// (boolean) - ignore sticky posts or not (optional).
$query->set('ignore_sticky_posts', false);
// (bool) Default is true - Post information cache (optional).
$query->set('cache_results', false);
// (bool) Default is true - Post meta information cache (optional).
$query->set('update_post_term_cache', false);
// (bool) Default is true - Post term information cache (optional).
$query->set('update_post_meta_cache', false);
In general, you should be careful when dealing with this because many things can affect your problem. You should also play with the priority of add_action
and increase the value above 20 or decrease it below 10.
It's also matter if you defined this in the theme or plugin. If is defined in a plugin, then it must be defined within 'plugins_loaded'
:
add_action( 'plugins_loaded', function( $query ) {
add_action( 'elementor/query/homepage_public_notices', function( $query ) {
$today = date_i18n ('Y-m-d');
$meta_key = 'relevant_datetime';
$query->set( 'meta_query', array(
array(
'key' => $meta_key,
'compare' => '=',
'value' => $today,
'type' => 'DATE',
)
) );
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC');
$query->set('meta_key', $meta_key);
// Retrieves posts by Post Status 'publish'
$query->set('post_status', 'publish');
// (boolean) - ignore sticky posts or not (optional).
$query->set('ignore_sticky_posts', false);
// (bool) Default is true - Post information cache (optional).
$query->set('cache_results', false);
// (bool) Default is true - Post meta information cache (optional).
$query->set('update_post_term_cache', false);
// (bool) Default is true - Post term information cache (optional).
$query->set('update_post_meta_cache', false);
}, 20 );
}, 20 );
I hope I helped you on this.