I’m trying to create a subcategory template of a defined category.
For example, for each one, I would like to check if his subcategory Photos exists and to get posts of this.

Here is my category tree:

— Category Sessions
— — Subcategory Session 2015
— — — Subcategory Photos
— — — Subcategory ...
— — — Subcategory Videos
— — Subcategory Session 2014
— — — Subcategory Photos
— — — Subcategory ...
— — — Subcategory Videos
— — Subcategory Session 2013
— — — Subcategory Photos
— — — Subcategory ...
— — — Subcategory Videos

If subcategory Photos of this session category exists :

My idea is to have a query such as :

$wp_query = new WP_query(... Session 20XX/Photos ...&showposts=20);

Is there a way to do that please?

1 Answer
1

// Getting Parent ID
$parent = get_term_by( 'name', 'Session 2015', 'category');

// Checking if SubCategory Exist. 
$term = term_exists('Photos', 'category', $parent->term_id);

//if Exist Do the query
if ($term !== 0 && $term !== null) {
  $query = new WP_Query( 'cat=".$term["term_id'] );
}

References:

  • term_exists

  • get_term_by

Leave a Reply

Your email address will not be published. Required fields are marked *