0

I am trying to get the custom posts "contact" with the custom taxonomy "abteilung" for this post type. Everything i am trying is not working. Now i am stuck with the following. The problem is, that the query is getting all the custom posts, not only that with the specific taxonomy id 29.

In the Backend everything is working. I can add taxonomy items to the custom posts and it is counting right the number of posts with the tax-items but however, my query is not working:

    $args= array(
        'post_type' => 'contact',
        'numberposts' => -1,
        array(
            array(
                'taxonomy' => 'abteilung',
                'field' => 'term_id',
                'terms' => 29
            )
        )
    );

    $query= new WP_Query($args);

    if($query-> have_posts()):
        while($query-> have_posts()): $query-> the_post();
            the_title(); //Just an example for the loop
        endwhile;
    endif; 

The custom posts and taxonomy i created like this:

function create_post_type() {
    register_post_type('contact',
        array(
        'labels' => array(
        'name' => __('Kontakte'),
        'singular_name' => __('Kontakt')
        ),
        'public' => true,
        'supports' => array('title'),
    )
    );

    register_taxonomy('abteilung', 'contact',
        array(
            'hierarchical'  => true,
            'label'         => __('Abteilung'),
            'query_var'     => true
        )
    );
}

add_action ('init', 'create_post_type');
  • The problem is in your query arguments, you're missing 'tax_query' => before the outer array containing the taxonomy arguments. See WP_Query Taxonomy Parameters for examples. – Milo Jan 16 '18 at 2:44
  • Oh yes thank you. I missed it totally in the example. – michaelsitte Jan 16 '18 at 9:41
0

Your taxonomy Query is worng. please try something like that:

$args = array(
    'post_type' => 'contact',
    'tax_query' => array(
        array(
            'taxonomy' => 'abteilung',
            'field'    => 'term_id',
            'terms'    => '29',
        ),
    ),
);
  • It works! :) I forgot totally forgot the " 'tax_query' => " before the array but generally i had a problem with missing commas. Thank you! – michaelsitte Jan 16 '18 at 9:43

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.