Sticky Posts exceed posts per page limit

Here is an approach to account for sticky posts by getting the number of sticky posts (if any) and include that in the calculation posts_per_page parameter: add_action(‘pre_get_posts’, ‘ad_custom_query’); function ad_custom_query($query) { if ($query->is_main_query() && is_home()) { // set the number of posts per page $posts_per_page = 12; // get sticky posts array $sticky_posts = get_option( … Read more

Using pre_get_posts with WP_Query

The simplest way is to add the action right before the query and remove it immediately after. add_action(‘pre_get_posts’, ‘some_function_in_functionsphp’); $my_secondary_loop = new WP_Query(…); remove_action(‘pre_get_posts’, ‘some_function_in_functionsphp’); if( $my_secondary_loop->have_posts() ): while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post(); //The secondary loop endwhile; endif; wp_reset_postdata(); EDIT Another technique you can use is to set your own query var and check for that … Read more

Should I use Pre Get Posts or WP_Query

pre_get_posts will run the same query, so both will take same time. But, If you utilize pre_get_posts action you will save one or more SQL queries. Right now, WordPress is running default query and then you run your query with this function which replace the results of the default query (resulting, default query is of … Read more

When to use WP_query(), query_posts() and pre_get_posts

You are right to say: Never use query_posts anymore pre_get_posts pre_get_posts is a filter, for altering any query. It is most often used to alter only the ‘main query’: add_action(‘pre_get_posts’,’wpse50761_alter_query’); function wpse50761_alter_query($query){ if( $query->is_main_query() ){ //Do something to main query } } (I would also check that is_admin() returns false – though this may be … Read more

tech