• I am so confused when it comes to using WP_Query $args to get the information I am looking for. My site has custom taxonomy. I am attempting to pull all like items based on the selected taxonomy terms the user saves. I think I have to use tax_query as part of the $args to get the result but I am stuck.

    Here is my call so far:

    $final_terms is an array of term names

    Array
    (
    [0] => 15 minutes or less
    [1] => 30 minutes of less
    [2] => Salad
    [3] => Snack
    [4] => Mexican
    [5] => Fruit
    [6] => Meat
    [7] => Breakfast
    [8] => Lunch
    [9] => Dinner
    )

    that match the name field in the wp_terms. Those terms match the group (taxonomy) in the wp_term_taxonomy table.

    $query_args = array(
    		'post_type' => 'recipe',
    		'paged' => $paged,
    		'paged' => $paged,
    		'tax_query' => array(
    			array(
    				'field' => 'name',
    				'terms' => $final_terms
    			)
    		),
    
    	);

    I need to pull all the recipes that share the same term.

Viewing 1 replies (of 1 total)
  • You also must include the ‘taxonomy’ value in the array. If your taxonomy name is ‘recipetype’, then the array would look like this:

    $query_args = array(
    		'post_type' => 'recipe',
    		'paged' => $paged,
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'recipetype',
    				'field' => 'name',
    				'terms' => $final_terms,
    				'operator' => 'IN',
    			)
    		),
    	);

    This would pull all recipes that have any one of the terms. If you want only recipes that have all of the terms, change the operator to ‘AND’.

Viewing 1 replies (of 1 total)
  • The topic ‘Using query_posts to get list of matching taxonomy’ is closed to new replies.