• Resolved Pete

    (@perthmetro)


    I don’t know why my custom taxonomy archive is displaying posts with the custom field “archives” and value “no”. Shouldn’t it only show those posts with a value of “yes”?

    // Pre get posts example
    add_action( 'pre_get_posts', 'tax_public' );
    function tax_public( $query ) {
        if (  is_taxonomy() && is_main_query() ) {
    			$query->set('posts_per_page', 30);
    			$query->set( 'orderby', 'date' );
    			$query->set('post__not_in', array(99999999999999999999999,99999999999999));
    			$query->set( 'meta_key', 'archives' );
    			$query->set( 'meta_value', 'yes' );    
        }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The problem is in your conditionals. They’ll never return true so your query var settings never happen.

    First of all, is_taxonomy() is deprecated, use taxonomy_exists(). Additionally, I believe this does not do what you think it does. It checks if a taxonomy is declared, not if the current query is a taxonomy query. I think you want $query->is_tax() if you are checking for a custom taxonomy. You should specify which taxonomy you want to check for. Or perhaps you want is_category() or is_tag()?

    Secondly, is_main_query() probably will not have the effect you want, I believe you want $query->is_main_query().

    Thread Starter Pete

    (@perthmetro)

    Thanks, here’s my updated code that works…

    add_action( 'pre_get_posts', 'tax_public' );
    function tax_public( $query ) {
        if (  $query->is_tax() && $query->is_main_query() ) {
    			$query->set('posts_per_page', 30);
    			$query->set( 'orderby', 'date' );
    			$query->set('post__not_in', array(2029999999999999,999999999999999911));
    			$query->set( 'meta_key', 'archives' );
    			$query->set( 'meta_value', 'yes' );    
        }
    }
    • This reply was modified 5 years, 6 months ago by Pete.
    Moderator bcworkz

    (@bcworkz)

    Nice! You’re welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘pre get posts query not obeying meta key’ is closed to new replies.