Advertisement
luisabarca

Custom tax query

Oct 13th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2. function recipes_get_posts($category)
  3. {
  4.     $args = array(
  5.         'numberposts' => 5, //Add all or pagination later
  6.         'orderby' => 'DESC', //Custom sort later?
  7.         'post_type' => 'recipes',
  8.         'recipes_category' => $category,
  9.         'post_status' => 'publish',
  10.                 'tax_query' => array(
  11.                                      'relation' => 'AND', // can be OR too
  12.                                      array(
  13.                                            'taxonomy' => 'rating',
  14.                                            'field' => 'slug', // you can use ID too
  15.                                            'terms' => get_query_var('your_var_from_form') // you can pass an array with terms or ID's too
  16.                                      ),
  17.                                      array(
  18.                                            'taxonomy' => 'calorie-range',
  19.                                            'field' => 'id',
  20.                                            'terms' => array( 103, 115, 206)
  21.                                      )
  22.                                ) // end tax query array
  23.           ); // end args
  24.  
  25.     $posts =  get_posts($args);
  26.     // posts into an array
  27.     $arr = array();
  28.     foreach ($posts as $post)  {
  29.       $listing = array();
  30.       // Customize content in array later, ID/Title for testing purposes
  31.         $listing['id'] = $post->ID;
  32.         $listing['title'] = $post->post_title;
  33.       $arr[] = $listing;
  34.     }
  35.  
  36.     header("Content-Type: application/json");
  37.     json_encode($arr);
  38. }
  39. ?>
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement