How to sort blogposts by post_date and if exits by a ACF field

138 views
Skip to first unread message

wer...@glinka.co

unread,
Jun 28, 2023, 6:19:01 PM6/28/23
to Minneapolis St. Paul WordPress User Group
I am trying to sort a blog list by it creating date and if a ACF field 'republish' exists by that field instead. This code sorts the posts in two groups but not either or...

$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => array(
'blog_post_fields_republished' => 'DESC',
'post_date' => 'DESC'
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'blog_post_fields_republished',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'blog_post_fields_republished',
'compare' => 'EXISTS'
)
),
'date_query' => array(
array(
'key' => 'blog_post_fields_republished',
'after' => 'date("F j, Y")',
)
),
'posts_per_page' => $number_blog_posts,
'paged' => $paged
);

Can anybody give a pointer on hjow to approach this properly?

Ambikesh Kumar Gautam

unread,
Jun 29, 2023, 12:49:07 PM6/29/23
to mpls-stpau...@googlegroups.com
It seems that you are trying to sort your blog posts by a custom field called 'republish' that is created using Advanced Custom Fields (ACF). However, your code is not working as expected and it is sorting the posts into two groups instead of one.

One possible reason for this issue is that you are using the 'date_query' parameter incorrectly. According to the WordPress documentationhttps://purpleturtlecreative.com/blog/2022/03/how-to-sort-wordpress-posts-by-acf-date-field-values/, the 'date_query' parameter is used to limit posts based on date and time. However, you are using it to query posts based on a custom field value, which is not supported by this parameter.

Instead, you should use the 'meta_query' parameter to filter posts by the custom field value. For example, if you want to show only posts that have a 'republish' date after today, you can use something like this:

'meta_query' => array(

array(
'key' => 'blog_post_fields_republished',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)

This will compare the custom field value with the current date in the format of YYYYMMDD, which is how ACF stores date values https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/. You can also use other formats and operators as needed.

Another possible reason for this issue is that you are using the wrong 'orderby' parameter. According to the ACF documentationhttps://tommcfarlin.com/sort-wordpress-posts/, if you want to sort posts by a date field, you should use 'meta_value_date' instead of 'meta_value'. This will ensure that the date values are sorted correctly.

For example, you can use something like this:


'orderby' => array(
'blog_post_fields_republished' => 'DESC',
'post_date' => 'DESC'
)

This will sort posts by the custom field value first, and then by the post date if the custom field values are equal.

We hope this helps you solve your problem. If you need more assistance, please feel free to reply or visit the WordPress or ACF forums for more support

--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpls-stpaul-wordpress/79e90818-2b18-4e2b-a4a7-05d099e8f3f7n%40googlegroups.com.

Werner Glinka

unread,
Jun 29, 2023, 1:38:55 PM6/29/23
to mpls-stpau...@googlegroups.com
@Ambikesh, thank you for your suggestions. Unfortunately, they don't work. Replacing my meta query with the one you have suggested results in Sorry, no posts matched your criteria. It would be nice if the help pages at https://developer.wordpress.org/reference/classes/wp_query/ would show an example for ordering by post date and a post meta field, but alas, no such luck. 

I think I'll get the posts and then do a sort in PHP that may not be as elegant as using a wp_query but is straightforward.



You received this message because you are subscribed to a topic in the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mpls-stpaul-wordpress/V3WOA3PH5tw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mpls-stpaul-word...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpls-stpaul-wordpress/CAH6AOoPGODuE9DwGu_mz9huNN2THkKHYnZYYXcF0OYpsWjNcdw%40mail.gmail.com.


Arago, David

unread,
Jun 30, 2023, 1:06:51 AM6/30/23
to mpls-stpau...@googlegroups.com
Hi, if regular WP query does not work, could you do a regulate sql query? Also, be careful of using WP query with the post_meta table.  That table is a beast, and it could slow down your queries to a crawl.

From: mpls-stpau...@googlegroups.com <mpls-stpau...@googlegroups.com> on behalf of Werner Glinka <wer...@glinka.co>
Sent: Thursday, June 29, 2023 12:38:41 PM
To: mpls-stpau...@googlegroups.com <mpls-stpau...@googlegroups.com>
Subject: Re: {MSPWP} How to sort blogposts by post_date and if exits by a ACF field
 

[External email – use care when clicking on links or attachments]

Reply all
Reply to author
Forward
0 new messages