What is “main query”? [duplicate]

Your filter has a bug in it, namely when you call is_main_query, you’re not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true. So instead try this: add_action( ‘pre_get_posts’, ‘some_name’); function some_name($query) { if ($query->is_front_page() && $query->is_main_query()) { $query->set( … Read more

multiple orderby in pre_get_posts action

As Milo said : $query->set(‘meta_key’, ‘wpcf-object-sold-status’ ); $query->set(‘orderby’, array(‘meta_value’ => ‘ASC’, ‘date’ => ‘DESC’)); // $query->set(‘order’, ‘ASC DESC’ ); // not needed Relevant link: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

Media library to list images only user uploaded

This works for me in order to list the items uploaded by a user on the media library. function users_my_media_only( $wp_query ) { if ( false !== strpos( $_SERVER[ ‘REQUEST_URI’ ], ‘/wp-admin/upload.php’ ) ) { $current_user = wp_get_current_user(); $current_user = $current_user->ID; if ( ! current_user_can( ‘manage_options’ ) ) { global $current_user; $wp_query->set( ‘author’, $current_user->id ); … Read more

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