HomeWORDPRESSWhat is the difference between pre_get_posts and query_posts()?

What is the difference between pre_get_posts and query_posts()?

1. pre_get_posts

Definition:

  • A filter hook that lets you modify the main query before it runs.
  • Runs early in the page load (before posts are fetched).
  • Preferred method for changing queries.

Example:

function modify_main_query( $query ) {
    if ( $query->is_main_query() && !is_admin() && $query->is_home() ) {
        $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'modify_main_query' );

Advantages:

  • Works without replacing the main query.
  • Maintains pagination and other WordPress logic.
  • More efficient.

2. query_posts()

Definition:

  • A template tag that modifies the main query by replacing it entirely.
  • Runs after WordPress has already created the main query.
  • Not recommended because it can break pagination and cause performance issues.

Example:

query_posts( array(
    'posts_per_page' => 5,
    'category_name' => 'news'
) );

Disadvantages:

  • Overrides the main $wp_query object.
  • Can mess up pagination and conditional tags.
  • Less flexible and older approach.

Key Differences Table

Featurepre_get_postsquery_posts()
TypeAction/filter hookTemplate tag
When RunsBefore main query executesAfter main query created
PerformanceEfficient, recommendedLess efficient, not recommended
PaginationPreserves paginationOften breaks pagination
Best UseModify main query parametersQuick, temporary query changes (rarely advised)

💡 Quick Memory Tip:

pre_get_posts = Adjust the recipe before cooking 🍲
query_posts() = Throw away the first dish and start over — wasteful and messy.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *