I need to get a complex result set from a WordPress database according to the category structure.
First I will try to explain my category structure. I have three level structure as bellow.
flight [level 1] [ID : 100] - class [level 2] [ID : 200] -- economy [level 3] [ID : 201] -- business [level 3] [ID : 202] -- first [level 3] [ID : 203] - alliance [level 2] [ID : 210] -- star [level 3] [ID : 211] -- oneworld [level 3] [ID : 212] -- skyteam [level 3] [ID : 213]
Now the algorithm:
I need to get all the posts tagged as
flight category or its one of the child with the following rules.
I need to exclude the posts tagged as
- However, it should still be in the result set if one of its siblings(
first) were tagged.
- It should not consider the posts where
allianceor its one of the child were tagged IF
economyalso tagged in the same posts [but this rule anyway full fill when we exclude the
economycategory in general]
My approach so far:
I was trying
tax_query with the following type of args
$args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => array( 100 ), 'include_children' => 1, ), array( 'relation' => 'AND', array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => array( 200 ), 'include_children' => 1, ), array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => array( 201 ), 'operator' => 'NOT IN', ), ), ), ); $query = new WP_Query( $args );
But the issue here is, it is not fulfilling the rule number 1. Even though I need to have posts with the tag of
first irrespective of
economy, the above query just ignores all the posts tagged with
I would even consider the pure
SQL approach as well.
Any help would be highly appreciating as I was struggling with this for a couple of days now.