Modify post filter to set custom number of posts per page and exclude child posts
Just call $query->set 2 times: $query->set(‘posts_per_page’, 48 ); $query->set(‘post_parent’, 0 );
Just call $query->set 2 times: $query->set(‘posts_per_page’, 48 ); $query->set(‘post_parent’, 0 );
pre_get_posts in your child themes functions file is the best in my opinion. add_action( ‘pre_get_posts’, ‘wpsites_custom_get_posts’ ); function wpsites_custom_get_posts( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( ‘cat’, ‘-182,-184’ ); $query->set( ‘posts_per_page’, ’12’ ); $query->set( ‘orderby’, ‘RAND’ ); } } This method is more flexible in my opinion.
Remove the action before you run the internal query, and always verify main query with is_main_query: function custom_loops($query) { if ( $query->is_tax( ‘service_photo_location’ ) && $query->is_main_query() ){ remove_action( ‘pre_get_posts’, ‘custom_loops’ ); $ids = get_nearby_partners_by_area(50000, ‘service_photo’); // … } } add_action(‘pre_get_posts’, ‘custom_loops’);
The logic inside the is_home function relies on the main query, but by definition that query hasn’t happened yet as you’re still in the pre_get_posts filter. Instead, consider $q->is_home(). The same goes for is_feed() $q->is_feed()
You can use the function wp_list_pluck to get the ID of the posts retrieved from the query. Just move the query before the h2 element and get the posts ID’s: // Set a bool to know if at least one post has post thumbnail. $has_thumb = false; $first_query = new WP_Query(‘cat=1&author=” . $post->post_author . “&order=DESC&tag=Tattoo&posts_per_page=1000’); … Read more
You could do this a few different ways, depending on what’s happening in the template and how you are able to identify which page you want to override. 1. Simple method with pre_get_posts: This will mostly work, but the queried object will still contain the old page, however, using get_the_ID() in the template to load … Read more
You can query a list of post IDs using $wpdb based on their slugs, and then use that in your filter. Here’s a simple example of how to do this: function mm_search_filter( $query ) { // We will query a list of post IDs // by using $wpdb global $wpdb; $post_ids = $wpdb->get_results(“SELECT ID FROM … Read more
Try this: EDIT: added short circuit return statements to avoid running unnecessary code on admin pages or other queries (e.g. nav menu items) // Custom query for research sub-categories function custom_query_for_research_subcategories($query) { if (is_admin() || !$query->is_main_query()) { return $query; } $qobj = get_queried_object(); if (isset($qobj->taxonomy) && ‘category’ == $qobj->taxonomy ) { $term = get_term($qobj->term_id); if … Read more
You’re missing an s. $query->set(‘posts_per_page’, ‘1’); However – you probably don’t want to do this for every query, since that affects everything – widgets, back end, everything. You should make it conditional. For example: <?php function university_adjust_queries($query) { // If this is the main query, not in wp-admin, and this is the homepage if($query->is_main_query() && … Read more
Like others have said you can’t use pre get posts since there is no way of knowing if the search returned any posts. I would also say that the if else is a very clean way to do it and might be the better choice if anyone else is going to be working on this. … Read more