Home › Forums › General Issues › WPQuery ACF True/False ordering issue
Hello,
I am willing to display post from default post type + an “event” CPT, everything being ordered by:
1. an ACF True/False field (featured posts)
2. default publishing date
Below is my query, which doesn’t display all featured elements first as expected (although get_field() does output correct True/False field value for each element).
$cdm_front_query_args = array(
'post_type' => array( 'post', 'events' ),
'posts_per_page' => 9,
'meta_key' => 'pin_home_page',
'orderby' => array( 'meta_value_num', 'date' ),
'order' => 'DESC',
);
$project_query = new WP_Query( $cdm_front_query_args );
I also tried disabling all plugins but ACF, without more success.
What could I be missing?
You cannot order by a meta value and date using ‘orderby` that way you are trying.
'meta_query' => array(
'featured_clause = array(
'key' => 'pin_home_page',
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC'
)
),
'orderby' => array('featured_clause' => 'DESC', 'date' => 'DESC');
do a search for “clause” on the WP_Query page.
Phew, finally found out what was wrong…
Query args should be as follows:
$cdm_front_query_args = array(
'post_type' => array( 'post', 'events' ),
'posts_per_page' => 9,
'meta_key' => 'pin_home_page',
'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ),
);
Oh thank you John, I just notice your answer after replying to my own thread.
It seems to be working with my snippet. Is it important to use the featured_clause as in your snippet ?
If what you’re doing is working I would go with it. I’ve just gotten used to using clauses because I usually ordering posts by more than one meta value. Using meta query clauses has become just the way I do everything. Never know when a client will want a change and it’s easier to rework the query if it’s already set up that way.
Ok for this query I won’t have more meta_key to add so i’ll leave my snippet (FYI it actually comes from the WP_Query Codex page – cf. subtitles “Mulitiple orderby/order pairs”).
Glad to read yours though as I am not familiar with multi-meta_key queries.
Thank you for your kind support.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.