in Wordpress template's function.php following code is working well
// Function accepting current query
function my_change_order( $query ) {
// Check if the query is for an archive
if($query->is_archive())
// Query was for archive, then set order
$query->set( 'order' , 'asc' );
// Return the query (else there's no more query, oops!)
return $query;
}
// Runs before the posts are fetched
add_filter( 'pre_get_posts' , 'my_change_order' );
But i need to order articles by custom meta key like _my_meta_vip
. Based on this answer i tried following lines, with half success, because only load articles with defined custom meta key, others are missing. How can i solve that?
function my_change_order( $query ) {
if($query->is_archive())
$query->set( 'orderby' , 'meta_value' );
$query->set( 'meta_key' , '_my_meta_vip' );
return $query;
}
add_filter( 'pre_get_posts' , 'my_change_order' );
How can i order my articles by custom meta key properly?