Home › Forums › General Issues › Filter query by custom field and sort orderby another custom field › Reply To: Filter query by custom field and sort orderby another custom field
Thanks for your response @matthewmcvickar.
My date field is a custom field created in ACF, not the post’s publish date.
So I tried a print_r()
of the $news_query but I’m really not sure what I’m looking for. ( nor is my knowledge of mysql all that good ).
I’m pasting the output here in the hope that you might spot the problem.
WP_Query Object
(
[query] => Array
(
[post_type] => news
[post_status] => publish
[posts_per_page] => 12
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[key] => archive
[value] => 1
)
[1] => Array
(
[key] => featured_article
[value] => 0
)
)
[meta_key] => date
[orderby] => meta_value_num
[order] => DESC
)
[query_vars] => Array
(
[post_type] => news
[post_status] => publish
[posts_per_page] => 12
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[key] => archive
[value] => 1
)
[1] => Array
(
[key] => featured_article
[value] => 0
)
)
[meta_key] => date
[orderby] => meta_value_num
[order] => DESC
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[lazy_load_term_meta] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
)
[meta_query] => WP_Meta_Query Object
(
[queries] => Array
(
[0] => Array
(
[key] => date
)
[1] => Array
(
[0] => Array
(
[key] => archive
[value] => 1
)
[1] => Array
(
[key] => featured_article
[value] => 0
)
[relation] => AND
)
[relation] => AND
)
[relation] => AND
[meta_table] => wp_postmeta
[meta_id_column] => post_id
[primary_table] => wp_posts
[primary_id_column] => ID
[table_aliases:protected] => Array
(
[0] => wp_postmeta
[1] => mt1
[2] => mt2
)
[clauses:protected] => Array
(
[wp_postmeta] => Array
(
[key] => date
[compare] => =
[alias] => wp_postmeta
[cast] => CHAR
)
[mt1] => Array
(
[key] => archive
[value] => 1
[compare] => =
[alias] => mt1
[cast] => CHAR
)
[mt2] => Array
(
[key] => featured_article
[value] => 0
[compare] => =
[alias] => mt2
[cast] => CHAR
)
)
[has_or_relation:protected] =>
)
[date_query] =>
[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1 AND (
wp_postmeta.meta_key = 'date'
AND
(
( mt1.meta_key = 'archive' AND mt1.meta_value = '1' )
AND
( mt2.meta_key = 'featured_article' AND mt2.meta_value = '0' )
)
) AND wp_posts.post_type = 'news' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_postmeta.meta_value+0 DESC LIMIT 0, 12
[posts] => Array
(
........
)
)
I’ve edited some of the empty entries out of it because it was even longer.
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.