0
votes

Is it possible to use pre_get_posts hook to target/modify the query on a specific page?

Currently using query_posts to do this :- <?php query_posts( array( 'meta_key' => 'epicredrank', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged, ) ); ?>

Trying to do the same thing with a pre_get_posts hook and it doesn't seem to be working. I was under the impression that it was indeed possible to target a specific page with pre_get_posts but perhaps I'm just doing it wrong?

function wpeddit_order($query) { 
if(is_page('id=2439')){ 
   $query->set('meta_key', 'epicredrank'); 
   $query->set('orderby', 'meta_value_num'); 
   $query->set('order', 'DESC'); 
} 
return $query; 
}
add_action('pre_get_posts','wpeddit_order');
1

1 Answers

1
votes

From the docs:

pre_get_posts cannot be used to alter the query for Page requests (page templates) because 'is_page', 'is_singular', 'pagename' and other properties (depending if pretty permalinks are used) are already set by the parse_query() method.