The code below is suppose to display all posts with "featured_listing" meta key above all of the standard posts.
When the below code is in the functions.php, it hides all posts without "featured_listing" meta.
I can't work out why its not working. Help would be appreciated.
function custom_special_sort( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// if is this the main query and is this post type of business
if ( (is_post_type_archive('business') ) || (is_tax ('location') ) ) {
// order results by the meta_key 'featured_listing'
$query->set( 'meta_key', 'featured_listing' );
$query->set( 'orderby', 'featured_listing' );
$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'custom_special_sort' );
I suspect this could be similar to this mentioned on stackoverflow but I'm really not sure :
the query is doing an inner join on the posts and postmeta tables, which is > why posts that don't have that particular post meta entry aren't being returned by the query. What you want is a left join
https://stackoverflow.com/questions/30361635/wp-archives-order-by-custom-meta-key