Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to make a form which filters CPT by multiple taxonomies? WordPress

  • SOLVED

Hello,

I'm trying to create a form for a sidebar which allows one to choose checkboxes from several taxonomies connected to a custom post type to allow users to sort, for example, recipes by rating, meal, and calorie range (e.g. "I'd like breakfast recipes that are 3 stars or higher under 500 calories).

Not sure how to construct it. Any help?

Below is some example code that was where I was getting started, then got a little lost in the whole multiple taxonomy issue:


<?php

function recipes_get_posts($category) {

$args = array(
'numberposts' => 5, //Add all or pagination later
'orderby' => 'DESC', //Custom sort later?
'post_type' => 'recipes',
'recipes_category' => $category,
'post_status' => 'publish'
);

$posts = get_posts($args);

// posts into an array
$arr = array();
foreach ($posts as $post) {
$listing = array();
// Customize content in array later, ID/Title for testing purposes
$listing['id'] = $post->ID;
$listing['title'] = $post->post_title;
$arr[] = $listing;
}
header("Content-Type: application/json");
json_encode($arr);
}

?>

Answers (2)

2011-10-13

Luis Abarca answers:

yep, you can change more examples here http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

This is a pastebin version http://pastebin.com/QzXUK2VB


<?php
function recipes_get_posts($category)
{
$args = array(
'numberposts' => 5, //Add all or pagination later
'orderby' => 'DESC', //Custom sort later?
'post_type' => 'recipes',
'recipes_category' => $category,
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'AND', // can be OR too
array(
'taxonomy' => 'rating',
'field' => 'slug', // you can use ID too
'terms' => get_query_var('your_var_from_form') // you can pass an array with terms or ID's too
),
array(
'taxonomy' => 'calorie-range',
'field' => 'id',
'terms' => array( 103, 115, 206)
)
) // end tax query array
); // end args

$posts = get_posts($args);
// posts into an array
$arr = array();
foreach ($posts as $post) {
$listing = array();
// Customize content in array later, ID/Title for testing purposes
$listing['id'] = $post->ID;
$listing['title'] = $post->post_title;
$arr[] = $listing;
}

header("Content-Type: application/json");
json_encode($arr);
}
?>

2011-10-13

Luis Cordova answers:

Warning: Be careful using get_posts if your use requires more like WP_query: [[LINK href="http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts"]]http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts[[/LINK]] I think WP_query will be more efficient as it will not set some other things that get_posts will set and thereby WP_query will be faster since you are using ajax probably to query this.

it is just the query part you need little help with

instead of get_posts use the real tool:
(updated it with the example above and commented it a bit)

$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'rating',
'field' => 'slug',
'terms' => get_query_var('checkboxSelectionSlug') // this you pass on the GET url param key
)
array(
'taxonomy' => 'calorie-range',
'field' => 'id',
'terms' => array( 103, 115, 206 ) // here you make 103 correspond to a range and so on
)
)
...
);
$query = new WP_Query( $args );


moreover

you will have to build this query parameterizable so that the checkboxes will get read upon submission and you will build the $args properly so constructing thereby the tax_query array.

by rating, meal, and calorie range , well just some idea:


... // init the relation
foreach(parameters as paramter) {
$taxonomyQueryBuilder[] = array( ...);
}

$args = array(
'tax_query' => $taxonomyQueryBuilder;
)


That should do. Thanks!