0

I need to exclude from a query a category and a few custom taxonomies.

Is this code right? This query don't exclude any post, the one with the category, or the custom taxonomy. What am I doing wrong?

<?php 
 $args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'ubicacion',
            'field' => 'slug',
            'terms' => array( 'ruperto-concha', 'estallido', 'vineta', 'vitrina', 'letrero', 'plenoempleo', 'primero' ),
                    'operator' => 'NOT IN'
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array( 'las-informasiones' ),
            'operator' => 'NOT IN'
        )
    )
 );
 $query = new WP_Query( $args );
 ?> 

Note: i need to exclude post with that category, or with any of those custom taxonomies. Any of both conditions.

Sorry for the bad english.

UPDATE

I intended to this code works

<?php 
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => array( 'las-informasiones' ),
                'operator' => 'NOT IN'
              )
     );
     $query = new WP_Query( $args );
     ?> 

even this:

<?php 
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                'taxonomy' => 'category__not_in',
                'field' => 'slug',
                'terms' => array( 'las-informasiones' ),
                )
     );
     $query = new WP_Query( $args );
     ?> 

and, don't. There is some problem in wp with wp-query and categories? Somebody knows?

MOAR INFO

That's my custom tax code. I've used an automatic generator.

function register_taxonomy_ubicacion() {

    $labels = array( 
        'name' => _x( 'Ubicaciones', 'ubicacion' ),
        'singular_name' => _x( 'Ubicación', 'ubicacion' ),
        'search_items' => _x( 'Hurgar en ubicaciones', 'ubicacion' ),
        'popular_items' => _x( ' Ubicaciones más usadas', 'ubicacion' ),
        'all_items' => _x( 'Todos', 'ubicacion' ),
        'parent_item' => _x( 'Parent Ubicación', 'ubicacion' ),
        'parent_item_colon' => _x( 'Parent Ubicación:', 'ubicacion' ),
        'edit_item' => _x( 'Editar', 'ubicacion' ),
        'update_item' => _x( 'Actualizar Ubicación', 'ubicacion' ),
        'add_new_item' => _x( 'Añadir Ubicación', 'ubicacion' ),
        'new_item_name' => _x( 'Nueva Ubicación', 'ubicacion' ),
        'separate_items_with_commas' => _x( 'separelas con comas', 'ubicacion' ),
        'add_or_remove_items' => _x( 'Añadir o remover ubicación', 'ubicacion' ),
        'choose_from_most_used' => _x( 'Escoger de las más usadas', 'ubicacion' ),
        'menu_name' => _x( 'Ubicaciones', 'ubicacion' ),
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => false,
        'show_admin_column' => true,
        'hierarchical' => false,

        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'ubicacion', array('post'), $args );
}
  • 1
    When you say "The output doesn't works", what do you mean. What doesn't happen, or what happens when it is not suppose to? Please edit your question – Pieter Goosen May 2 '14 at 20:05
  • done, my friend. – Li Chong May 2 '14 at 20:07
  • I'm not sure why the above isn't working but you might want to try using pre_get_posts with a function instead? codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts – Michelle May 2 '14 at 20:35
  • the pre_get_posts exclude the category but the tax_query don't work anymore. – Li Chong May 2 '14 at 21:08
1

Change 'relation' => 'OR' to 'relation' => 'AND' to apply both exclusions to all posts returned. It may seem counterintuitive, but a post that satisfies one of the exclusions will be included even if it doesn't satisfy the other.

EDIT

From the code linked in your comment below, this is the query being generated:

SELECT SQL_CALC_FOUND_ROWS duk_posts.ID
FROM duk_posts
WHERE 1=1 AND 0 = 1
AND duk_posts.post_type = 'post'
AND (duk_posts.post_status = 'publish'
OR duk_posts.post_status = 'private')
GROUP BY duk_posts.ID
ORDER BY duk_posts.post_date
DESC LIMIT 0, 10

as you can see, there's nothing related to category or taxonomy there, and the WHERE clause is an invalid condition that could never be true. This leads me to believe that your custom taxonomy is not being registered correctly, so when the query is run it is seen as an invalid taxonomy and therefore creates this invalid query. I'll guess you register the taxonomy on the wrong hook or somehow have the name wrong.

| improve this answer | |
  • Not, same result. Doesn't exclude any post. – Li Chong May 2 '14 at 21:04
  • have a look at var_dump($query) after it is run and check the query parameters, you may have another filter modifying them. – Milo May 2 '14 at 22:28
  • Done. This is the output. If any of you could find the problem before than me, i will be very grateful. pastebin.com/EicsFh7n – Li Chong May 3 '14 at 2:41
  • see my edit above. – Milo May 5 '14 at 1:14
  • that's very strange. By the Way, how do you reconstruct the query? And, in my edit, the query to a cat doesn't worked, but the cpt it does – Li Chong May 5 '14 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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