Custom queries and sticky posts are quite a curve ball to work with. I don’t know how your setup looks and what exactly is your user case, but your best solution here would be to run two queries here, the first one to get your sticky posts and the other one to display normal posts
Your first query’s arguments will look something like
$args = array(
'post_type' => 'myposttype',
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
'meta_query' => array(
'relation' => 'AND',
array('key' => 'routeFrom','value' => 'rome','compare' => 'LIKE'),
array('key' => 'routeTo','value' => 'paris','compare' => 'LIKE'),
)
);
Your second query arguments you will need to exclude these stickies
$args = array(
'post_type'=> 'myposttype',
'posts_per_page' => 10,
'post__not_in' => get_option( 'sticky_posts' ),
'meta_query' => array(
'relation' => 'AND',
array('key' => 'routeFrom','value' => 'rome','compare' => 'LIKE'),
array('key' => 'routeTo','value' => 'paris','compare' => 'LIKE'),
)
);
Just make sure that you reset both queries.
As I said before, I do not know your exact setup and user case, but if this is suppose to be the main query, you should look at pre_get_posts
to alter the main query accordingly. If so, look at this post I have recently done to include sticky posts outside the home page.
EDIT
I totally forgot to add ignore_sticky_posts
to the first set of query arguments. It should work now. I have updated the code accordingly
Just one or two notes on your code
-
You should use
wp_reset_postdata()
to reset your queries, notwp_reset_query()
. The latter is used withquery_posts
which you should never use. Also,wp_reset_postdata()
should be used betweenendwhile
andendif
-
To exclude the sticky post query from paged pages, simply warp the code in a
if ( !is_paged() ) { YOUR STICKY POST CODE }
condition