• Resolved charlesolaes

    (@charlesolaes)


    I read the wordpress codex and it said I could use wp_query and tax_query parameters to bring post that are in that taxonomy but it doesn’t seem to be filtering correctly, it still brings back all the posts I asked for.

    here’s my code

    $args = array( 'tax_query' => array(
                'taxonomy' => $this->taxonomy_name,
                'field' => 'slug',
                'terms' => $category
            ), 'post_type' => $this->post_type_name );
    
            $myposts = new WP_Query($args);
            /*
            $args output: Array ( [tax_query] => Array ( [taxonomy] => our_works [field] => slug [terms] => commercial ) [post_type] => dlm_works )
            */

    I did a print_r() on the $mypost->posts variable and it returns the ALL the posts set in the post_type, when I remove the ‘post_type’ => $this->post_type_name filter, it return ALL the posts I have.

    I used get_terms on my post just to make sure and here’s the result.

    global $post, $dlm_project_var;
    print_r(get_the_terms($post->ID, $dlm_project_var->taxonomy_name));
    
    /*
    output: there are two custom posts for that taxonomy
    1: Array ( [23] => stdClass Object ( [term_id] => 23 [name] => Websites [slug] => website [term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => our_works [description] => [parent] => 0 [count] => 1 [object_id] => 239 ) ) 
    
    2:Array ( [22] => stdClass Object ( [term_id] => 22 [name] => Commercials [slug] => commercial [term_group] => 0 [term_taxonomy_id] => 22 [taxonomy] => our_works [description] => [parent] => 0 [count] => 1 [object_id] => 205 ) )
    */

    I have no idea why tax_query isn’t working, I’ve been starring at the codex to make sure I got the syntax right but I still have no clue what’s going on…

    Please help

Viewing 2 replies - 1 through 2 (of 2 total)
  • My first guess would be that you are telling WordPress that you’re going to supply the taxonomy “slug”, but it looks like you’re actually providing it with the taxonomy “name”.

    I am also not sure where you are defining the $taxonomy_name variable for the class in which you’re using it, so I can’t say for sure what you are sending to the query.

    Thread Starter charlesolaes

    (@charlesolaes)

    thanks curtiss, turns out my query was a bit wrong, there was suppose to be an array within that array so the corrent syntax should be.

    $args = array( 'tax_query' => array(
               //another array
               array( 'taxonomy' => $this->taxonomy_name,
                'field' => 'slug',
                'terms' => $category)
            ), 'post_type' => $this->post_type_name );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘getting posts from custom taxonomy’ is closed to new replies.