get_query_var( ‘paged’ ) not working outside of homepage

twentyeleven_content_nav() uses the main query object, $wp_query. You’ll need to use the $wp_query variable, rather than $unfiltered_query, then wp_reset_query() to restore the original $wp_query (which it’ll find in $wp_the_query, something you should probably avoid touching directly). As long as you’re careful to restore the original query, you’re in good shape. I would submit a patch … Read more

Show posts without term

You cannot use the same object of WP_Query twice. Therefore you need to create another one with a tax_query parameter to fetch posts which are not assigned to any term. //fetch all reviews which have no assigned term in ‘review-product’ $taxonomy = ‘review-product’; $post_type=”reviews”; $args = [ ‘post_type’ => $post_type, ‘tax_query’ => [ [ ‘taxonomy’ … Read more

WP_Query order by multiple meta keys & fields

You are using meta query without setting a value. The way you are doing it is using to query posts, not to order them. Using Named Meta Queries To order your posts by different meta datas, you can give your meta queries a name and then use that to set the ordering. Here is a … Read more

get_query_var function not working at all

WordPress doesn’t automatically add all query string params ($_GET params) as query_vars. When Query Var somevar is not registered: example.com/some-page/?somevar=hello – WordPress ignores somevar When Query Var somevar is registered: example.com/some-page/?somevar=hello – WordPress stores the value of this param in the $wp_query->query_vars array The difference between registering that variable with WordPress is whether the value … Read more

WP_Query: get 3 random posts from 10 latest

There’s one way with: $args = [ ‘post_type’ => ‘post’, ‘posts_per_page’ => 10, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘no_found_rows’ => ‘true’, ‘_shuffle_and_pick’ => 3 // <– our custom argument ]; $query = new \WP_Query( $args ); where the custom _shuffle_and_pick attribute is supported by this demo plugin: <?php /** * Plugin Name: Support for … Read more

Exclude or Include category ids in WP_Query

As you probably know it, categories are taxonomies. When you use the arguments such as category__in, it will add a tax query to your WP_Query(). So, your situation would be something like this: $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘category’, ‘field’ => ‘term_id’, ‘terms’ => array( … Read more

Prevent pre_get_posts filter on specific post type

Improve your conditional to include a check for post type being queried. It can be done via WP_Query::get method So, where you have if ( !is_admin() ){ $query->set( ‘meta_key’, ‘_ct_selectbox_52f65ae267764’ ); $query->set( ‘meta_value’, $city ); return; } replace with if ( ! is_admin() && in_array ( $query->get(‘post_type’), array(‘event’,’venue’) ) ) { $query->set( ‘meta_key’, ‘_ct_selectbox_52f65ae267764’ ); … Read more

Order by DESC, ASC in custom WP_Query

Try this: $args = array( ‘post_type’ => ‘post’, ‘meta_key’ => ‘pb_issue_featured’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘DESC’, ‘posts_per_page’ => $posts, ‘paged’ => $paged, ‘paged’ => 1, ‘meta_query’ => array( array( ‘key’ => ‘headline’, ‘value’ => 1, ‘compare’ => ‘!=’ ) ) ); add_filter( ‘posts_orderby’, ‘filter_query’ ); $q = new WP_Query($args); remove_filter( ‘posts_orderby’, ‘filter_query’ ); function … Read more

tech