Why is ‘pre_get_posts’ having no effect?

… It’s a very simple piece of code on the index page of an
otherwise empty theme…

If you are running this code on the index page of your theme and expect it to effect the main query, then you are adding the action too late. The main query runs well before your theme template loads. You will need to put that code in functions.php in your active theme or in a plugin and use some conditional logic to make sure that it runs only where you want it too. For example…

function just_one( $query ) {
  if ( $query->is_main_query() && $query->is_front_page() ) {
    $query->set( 'posts_per_page', 1 );
  }
}
add_action( 'pre_get_posts', 'just_one' );

I don’t really know what the if condition should be. Refer to the Codex page already linked to for the conditions available.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)