Can you try by re writing your code as under ?
$exec_query = new WP_Query( array (
'post_type' => 'cars',
'car_type' => 'large_cars',
'posts_per_page' => 100,
'order' => 'ASC',
'tax_query' => array(
array(
'include_children' => false
)
),
) );
//* The Loop
if ( $exec_query->have_posts() ) { ?>
<?php
while ( $exec_query->have_posts() ): $exec_query->the_post();
get_template_part( 'parts/loop', 'single-produkt' );
endwhile; ?>
<?php
//* Restore original Post Data
wp_reset_postdata();
}
-
This reply was modified 9 years, 2 months ago by
LumberHack.
Thread Starter
pmbs
(@pmbs)
Thank you for your suggestion, but when adding the ‘tax_query’ part no posts at all show up :/
Thread Starter
pmbs
(@pmbs)
Might it have something to do with that “large_cars” has a parent itself?
That’s not it, LumberHack was on the right track, but you need to move all the tax query terms to within the tax_query argument array. Actually, the generic {$taxonomy} => {$term} argument style you started with is now discouraged (though the generic style still works… for now) over using 'tax_query' for all custom taxonomy query arguments. So that one instantiation statement should be more like this:
$exec_query = new WP_Query( array (
'post_type' => 'cars',
'posts_per_page' => 100,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'car_type',
'field' => 'slug',
'terms' => 'large_cars',
'include_children' => false,
),
),
) );
While the tax_query syntax is lot more convoluted, it is infinitely more flexible, so it’s well worth learning to use it. For starters, the include children argument is just not possible with the old generic style, you must entirely use tax_query for that, mixing styles does not work.
Thread Starter
pmbs
(@pmbs)
Thank you bcworkz. This seems that a more simple solution than the one I came up with yesterday (which also works though :))
$term_id = get_term_by( 'slug', $term, $taxonomy)->term_id;
$taxonomy_name = $taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
$exclude = "";
foreach ( $termchildren as $child ) {
$term2 = get_term_by( 'id', $child, $taxonomy_name );
$exclude = $exclude . "" . $term2->term_id . ",";
}
$exclude = substr($exclude, 0, -1);
$args = array(
//Rest of you args go here
'tax_query' => array (
array(
'taxonomy' => $taxonomy, // My Custom Taxonomy
'terms' => explode(',', $exclude), // My Taxonomy Term that I wanted to exclude
'field' => 'id', // Whether I am passing term Slug or term ID
'operator' => 'NOT IN', // Selection operator - use IN to include, NOT IN to exclude
),
),
'post_type' => $type,
$taxonomy => $term,
'posts_per_page' => 100,
'order' => 'ASC',
);
Hmmm, IMHO I would not call that “more simple”. The approach does allow finer control of what to exclude. For example, if there was one particular child term that you did want to include, you could add code to explicitly not add that one ID to the exclusion list.
For average sized sites, it probably makes little difference, but by calling get_term_by() inside a loop means this approach potentially makes many more DB queries than your original. On really large sites, doing so could noticeably slow down page load times. There are some opportunities to streamline the latter code so the difference diminishes, but I suspect the former is still more efficient. There could be more to the first query than we realize, maybe when we use 'include_children' => false, it causes something similar to what you are proposing to happen behind the scenes. If it were important enough to be absolutely most efficient, it’d be worth doing some benchmarking to be sure, because I’m only speculating, I don’t really know for sure which is most efficient.
In most cases, doing whatever works will be good enough. If you’re happy with the latter attempt, then go with it, it’ll be fine 🙂