0

I have read this post: Include and Exclude Taxonomies From Archives & Feeds Using 'pre_get_posts'

And then I have used that function to my theme. My Taxonomy (tinh-trang) had 3 term: 19,20,21 And I want to exclude all post have terms id: 19, 21 when visitor go to any archives.

But I have nothing changed?

add_filter('pre_get_posts','change_archive_loop');

function change_archive_loop( $query ) {
    $choduyet = intval(get_option('zen-id_cho_duyet')); // Get term ID 1 from theme option
    $baocao = intval(get_option('zen-id_bao_cao')); // Get term ID 2 from theme option
    if ( $query->is_tax( 'tinh-trang' ) && $query->is_main_query() ) {
        $terms = array($choduyet, $baocao);
        $query->set( 'post_type', array( 'post' ) );
        $query->set( 'tax_query', array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'tinh-trang',
                'field' => 'id',
                'operator' => 'NOT IN',
                'terms' => $terms
            )
        ) );
    }

    return $query;
}

2 Answers 2

0

Few remarks:

  • I use pre_get_posts as an action hook, not as a filter.
  • You should use ! is_admin() to prevent this from changing your backend views.
  • You should skip the 'relation' => 'OR', since you only have a single tax_query filter
  • Is there any reason why you have to modify the post type ? Is your custom taxonomy attached to multiple post types?

so you could try this modification instead:

add_action( 'pre_get_posts', 'change_archive_loop' );

function change_archive_loop( $query ) {
    $choduyet = intval( get_option( 'zen-id_cho_duyet' ) );
    $baocao = intval( get_option( 'zen-id_bao_cao' ) );
    if ( ! is_admin() && $query->is_tax( 'tinh-trang' ) && $query->is_main_query() ) {
        $terms = array( $choduyet, $baocao );
        $query->set( 'tax_query', array(
            array(
                'taxonomy' => 'tinh-trang',
                'field' => 'id',
                'operator' => 'NOT IN',
                'terms' => $terms
            )
        ) );
    }
 }
3
  • This works on my install in a custom taxonomy archive and excludes the selected terms. Can you add some debug info, like the var_dump on $terms?
    – birgire
    Jun 19, 2013 at 17:25
  • Thanks you very much! I have a problem in that function, so when I change $query->is_tax( 'tinh-trang' ) to $query->is_category() it's works! Jun 19, 2013 at 17:26
  • ok great to hear you got it working ;-)
    – birgire
    Jun 19, 2013 at 17:28
0

You can use exclude like this.

    $posts = get_posts(  array( 'numberposts'   => -1,
                                'exclude'       => array('post_id_1', 'post_id_2'),
                                'post_type'     => 'post_type',
                                'orderby'       => 'meta_value_num',
                                'meta_key'      => 'post_meta_key',
                                'order'         => 'DESC',
                                'tax_query'     => array('relation'=> 'OR',
                                                        array(  'taxonomy'  => 'taxonomy_1',
                                                                'field'     => 'id',
                                                                'terms'     => 'taxonomy_id_1'
                                                                )),
                                                        array(  'taxonomy'  => 'taxonomy_id_2',
                                                                'field'     => 'id',
                                                                'terms'     => 'taxonomy_id_2'
                                                                )),
                                'meta_query'    => array(array( 'key' => 'meta_key',
                                                                'value' => 'meta_value'
                                                            ))
                                            )
                                    );

If you want to use relation in 'meta_query' also then write the same code as write in 'tax_query'.

You can also use 'wp_query' instead of 'get_posts'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.