0

I'm not able to use array for the terms in the following query, only a single term:

    $args=array(
    'facts' => 'information', //taxonomy:facts and term:information
    'post_type' => 'book',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args)

Is this normal behiavour? Is there any equivalent method for selecting posts with certain terms from custom taxonomy with custom post type?

1

You're using a deprecated method of querying by taxonomy. Read the Codex and use tax_query:

$args=array(
    'post_type' => 'book',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'facts',
            'field' => 'slug',
            'terms' => 'information'
        )
    )
);

FYI, caller_get_posts is also deprecated.

1

I'm not sure if this is the correct method (as I was not getting the results I wanted by supplying the 'relation' parameter for the 'tax_query' one), but we have a scenario at my work, where the site uses a taxonomy called 'topics' as the main menu, so it needed to be handled separately from the rest of the taxonomies, which were being used as lower level filtering options (i.e. 'schools') along with filtering for 'post_type'. Here's my solution, following mrwweb's example.

What's important to note is that if the query variable for 'schools' was empty, I got no results for the entire query, when it should produce all results, as 'schools' is no longer being filtered. So, I just through that part of the query array into a conditional, and it worked as expected.

function uci_get_taxonomies() {
    $params = array();

    $topic = get_queried_object();
    $params[TAXONOMY_TOPICS] = $topic->slug;

    $contentTypes = get_query_var('post_type');
    $params['post_type'] = $contentTypes;

    $schools = get_query_var(TAXONOMY_SCHOOLS);
    if(!empty($schools)) {
        $params['tax_query'][] = array('taxonomy' => TAXONOMY_SCHOOLS, 'field' => 'slug', 'terms' => $schools);
    }

    $query = new WP_Query($params);
    //Util::debug($query->query_vars);

    return $query;
}

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.