I’ve managed to get wordpress to do a descending order on a meta_query field called start_date
which is saved in Ymd
format in the database by the ACF plugin.
However in my query the other post types that doesn’t have this field are unordered and also seems to always come after all the posts which contain the meta field.
Example on how the order currently is returned:
Post type: | Date: |
---|---|
News | 19 September 2020 |
News | 12 March 2019 |
News | 5 June 2018 |
Publication | 16 October 2020 |
Publication | 19 October 2020 |
Publication | 2 November 2020 |
This is an extract on the pre_get_posts hook i’m using.
function orderby_fieldifexists($orderby) {
return "mt1.post_id IS NOT NULL DESC, wp_postmeta.meta_value DESC, wp_posts.post_date DESC";
}
add_filter("posts_orderby", "orderby_fieldifexists", 10, 1);
$meta_query = [
'relation' => 'OR',
'start_date_value' => [
'key' => 'start_date',
],
'start_date' => [
'key' => 'start_date',
'compare' => 'NOT EXISTS'
]
];
$query->set('meta_query', $meta_query);
Does anyone have any clue on why this might be happening? I’ve been stuck with this issue for way too long time now and I’m asking here if anyone else might spot what I’m doing wrong.
Here’s also the SQL output from the query (that contains a few chunks of translation joins from wpml):
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
LEFT JOIN wp_postmeta AS mt1
ON (wp_posts.ID = mt1.post_id
AND mt1.meta_key = 'start_date' )
LEFT JOIN wp_icl_translations wpml_translations
ON wp_posts.ID = wpml_translations.element_id
AND wpml_translations.element_type = CONCAT('post_', wp_posts.post_type)
WHERE 1=1
AND (((wp_posts.post_title LIKE '%Test%')
OR (wp_posts.post_excerpt LIKE '%Test%')
OR (wp_posts.post_content LIKE '%Test%')))
AND ( wp_postmeta.meta_key = 'start_date'
OR mt1.post_id IS NULL )
AND wp_posts.post_type IN ('post', 'page', 'news', 'publication', 'event')
AND (wp_posts.post_status="publish"
OR wp_posts.post_status="acf-disabled"
OR wp_posts.post_author = 1
AND wp_posts.post_status="private")
AND ( ( ( wpml_translations.language_code="en"
OR ( wpml_translations.language_code="en"
AND wp_posts.post_type IN ( 'project' )
AND ( ( (
SELECT COUNT(element_id)
FROM wp_icl_translations
WHERE trid = wpml_translations.trid
AND language_code="en" ) = 0 )
OR ( (
SELECT COUNT(element_id)
FROM wp_icl_translations t2 JOIN wp_posts p
ON p.id = t2.element_id
WHERE t2.trid = wpml_translations.trid
AND t2.language_code="en"
AND ( p.post_status="publish"
OR p.post_type="attachment"
AND p.post_status="inherit" ) ) = 0 ) ) ) )
AND wp_posts.post_type IN ('post','page','attachment','wp_block','news','publication','event','project' ) )
OR wp_posts.post_type NOT IN ('post','page','attachment','wp_block','news','publication','event','project') )
GROUP BY wp_posts.ID
ORDER BY mt1.post_id IS NOT NULL DESC, wp_postmeta.meta_value DESC, wp_posts.post_date DESC
LIMIT 0, 10