0

I've using the Events Manager plugin which creates a custom post type of 'event' and a custom taxonomy called 'event-categories'.

I have the category 'courses' which has an ID of 43. I also have a number of child categories under courses e.g. 'science', 'english' etc.

I'm creating an autocomplete search and need to query courses based on the search term but only on posts that appear in the the post type of event, taxonomy of 'event-categories' and are tagged with the 'courses' category or any of its children.

Here is what I have so far:

   global $wpdb, $table_prefix;
   $em_suggestions = $wpdb->get_results("
      SELECT post_name as value, post_title as label
      FROM {$table_prefix}posts 
      WHERE post_type = 'event' 
      AND post_status = 'publish' 
      AND post_title LIKE '%$term%' 
      ORDER BY post_title ASC
      LIMIT 0, 10
   ");

I'm just struggling to apply the taxonomy filter to it.

1

tax_query is used to get the posts associated with certain taxonomy.

  • {tax} (string) - use taxonomy slug. Deprecated as of Version 3.1 in favor of 'tax_query'.
  • tax_query (array) - use taxonomy parameters (available with Version 3.1).
    • taxonomy (string) - Taxonomy.
    • field (string) - Select taxonomy term by ('id' or 'slug')
    • terms (int/string/array) - Taxonomy term(s).
    • include_children (boolean) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
    • operator (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'event-categories',
            'field' => 'id',
            'terms' => 43
        )
     )
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post;
        // do something
    }
}
  • Would you mind adding a little bit on why this is doing what was asked? This way, your answer will be of more help than just a code block without any other word. – tfrommen Nov 5 '13 at 11:32
  • Thanks for the reply, Adnan. Will this code deal with posts not tagged with the parent category of 'courses' but tagged with one of its childen, such as 'science'? – Chris Nov 5 '13 at 11:45
  • @Chris yes this code can deal with the above functionality in that case you have to pass the id of child category i.e science category has an id 44 'terms' => 44 – Adnan Nov 5 '13 at 12:05
  • Thanks Adnan. At first I couldn't get the taxonomy filter working, but looking at the codex, tax_query has to be within an extra array i.e. 'tax_query' => array(array('taxonomy' => 'event-categories','field' => 'id','terms' => 43)). Would you like to update the answer, then I'll accept it :) – Chris Nov 5 '13 at 12:16

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.