I assume that by “direct children of category” you mean “posts directly assigned to the category” and not “posts assigned to child categories of the category specified”.
The answer is in the Codex:
Display posts that have this category (not children of that category),
using category id:$query = new WP_Query( 'category__in=4' );
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Second, do not use query_posts()
. It clobbers several globals
and just makes a mess. Use WP_Query
.
$q = new WP_Query(
array(
'category__in' => $cat,
)
);
That will create a secondary Loop. To alter the main query, you need to hook into pre_get_posts
, as explained in the Codex, again on the query_posts()
page:
query_posts()
is overly simplistic and problematic way
to modify main query of a page by replacing it with new instance of
the query. It is inefficient (re-runs SQL queries) and will outright
fail in some circumstances (especially often when dealing with posts
pagination). Any modern WP code should use more reliable methods, like
making use ofpre_get_posts
hook, for this purpose.
In other words:
function no_cat_kids_wpse_143042( $query ) {
if ( $query->is_main_query() && $query->is_front_page() ) {
$query->set( 'category__in', 123 );
}
}
add_action( 'pre_get_posts', 'no_cat_kids_wpse_143042' );
I don’t know what conditionals you need– this, if ( $query->is_main_query() && $query->is_front_page() )
— and I don’t know how you are generating the $cat
variable, but that is the idea.
Place that code in functions.php
or a plugin. It has to execute before the page template loads to effect the main query.