How to select posts from one category but exclude posts in another category?

Use pre_get_posts to exclude the categories you don’t want to display in the loop. function exclude_posts_from_specific_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘cat’, ‘-2’ ); } } add_action( ‘pre_get_posts’, ‘exclude_posts_from_specific_category’ ); Or create a new WP_Query and use the Category Parameters. <?php $args = array( ‘category__not_in’ => 2 , ‘category__in’ … Read more

How to limit total number of posts in wp query loop?

You can use the found_posts filter to alter the number of posts WordPress reports finding from a query. add_filter( ‘found_posts’, ‘wpd_found_posts’, 10, 2 ); function wpd_found_posts( $found_posts, $query ) { if ( $query->is_home() && $query->is_main_query() ) { return 25; } }

Multiple instances of Featured Image Query

For both loops move <a href=”https://wordpress.stackexchange.com/questions/67673/<?php the_permalink(); ?>”> To after while ( $the_query->have_posts() ) : $the_query->the_post(); the_permalink() is template tag and as such generates what it does, based on global $post variable. In your custom loop it doesn’t contain the post you want until after $the_query->the_post() call.

Pagination working everywhere but the index page

Using Milo’s suggestion in the comments above fixed the issue. Using pre_get_posts we modify the loop like so. function filter_main_query( $query ) { if( is_front_page() && is_main_query() ) { $query->set(‘meta_key’, ‘rating’); $query->set(‘meta_value’, 0 ); // rest of params } } add_action(‘pre_get_posts’, ‘filter_main_query’, 9999);

Implementing an OR statement to wordpress wp_query

You can run two queries and merge the results: $cat_query_args = array( ‘paged’ => $paged, ‘order’ => ‘DESC’, ‘meta_key’ => ‘android_link’, ‘post_type’ => array( ‘post’, ‘app’ ), ‘cat’ => ’63’, ); $meta_query_args = array( ‘meta_query’ => array( array( ‘key’ => ‘android_link’, ‘compare’ => ‘EXISTS’, ) ) ); $cat_query = new WP_Query( $cat_query_args ); $meta_query = … Read more

Page navigation doesn’t show when query category

I found a partial workaround. Since the query object in the index.php is the right one I transferred it using serialize to template of choice, in this case category-video.php. In index.php i put on top <?php $s = serialize($wp_query); file_put_contents(‘query’,$s); ?> and in category-video.php I put <?php $u = file_get_contents(‘query’); $wp_the_query = unserialize($u); ?> It’s … Read more

When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?

You want to make sure you’re only messing with the main query and not subsequent queries called from a plugin or in a template on the search page. In other words… if( !is_admin() && $query->is_search ) { should be if( !is_admin() && $query->is_search && $query->is_main_query() ) { See http://codex.wordpress.org/Function_Reference/is_main_query