I'm trying to display the get_category_category limiting the results to only the child categories of parent "666".
So a post may have a number of categories e.g. "1234", "666", "666/1341", "5", "5/17" but I only want to show the child of "666".
$args = array( 'numberposts' => '5','post_status' => 'publish','category' => '1234','category__not_in' => '680', 'orderby' => 'post_date', 'order' => 'DESC' );
$recent_posts = wp_get_recent_posts($args);
{
///I want to return the category for each post in this loop that is within the "666" category.
///E.g get_the_category($recent['ID']) where the category is a child of 666
echo "POST ID" . $recent['ID'] . " CHILD CATEGORY ". $childcategory;
}
The above loop will return the last 5 posts in category "1234" with each child category of "666".
e.g.
I'm trying to display the get_category_category limiting the results to only the child categories of parent "666".
So a post may have a number of categories e.g. "1234", "666", "666/1341", "5", "5/17" but I only want to show the child of "666".
$args = array( 'numberposts' => '5','post_status' => 'publish','category' => '1234','category__not_in' => '680', 'orderby' => 'post_date', 'order' => 'DESC' );
$recent_posts = wp_get_recent_posts($args);
{
///I want to return the category for each post in this loop that is within the "666" category.
///E.g get_the_category($recent['ID']) where the category is a child of 666
echo "POST ID" . $recent['ID'] . " CHILD CATEGORY ". $childcategory;
}
The above loop will return the last 5 posts in category "1234" with each child category of "666".
e.g.
You can do this by getting the child categories of that category first, then passing them as an array to your query.
$categories=get_categories(
array( 'parent' => $cat->cat_ID )
);
$cat_list = [];
foreach ($categories as $c) {
$cat_list[] = $c->term_id;
}
ref: Get the children of the parent category
This won't/might not work, but it's usually worth trying passing an array where you could pass an integer in WordPress:
$args = array( 'numberposts' => '5','post_status' => 'publish','category' => $cat_list,'category__not_in' => '680', 'orderby' => 'post_date', 'order' => 'DESC' );
wp_get_recent_posts
uses get_posts
& get_post
accepts an ID of a category, at the time of writing it doesn't mention an array... So you might need to use WP_Query
:
$query = new WP_Query( array( 'category__and' => $cat_list ) );
see: https://codex.wordpress/Class_Reference/WP_Query
You can use array_map
so you can return only the categories that you want. For example:
array_map( function( $cat ) {
if ( $cat->parent != 0 ) {
return $cat;
}
},
get_the_category()
);