Reputation: 1911
The following code will print an array with all posts with custom field 'job_location' as 'US'. Its works well and shows post title, description and all data relate to each post.
My requirement is, I want to get the category name of each post. I dont want to accomplish this by looping the posts and get the category details of each posts, since that will need extra db access. So Is there any way to get the category details in 'get_posts' function itself?
$meta_query =array('key' => 'job_location','value' => 'US');
$post_args = array(
'post_type' => 'post', 'post_status' => 'publish','suppress_filters' => true, 'meta_query' => $meta_query
);
$posts_list = get_posts( $post_args );
var_dump($posts_list);
Upvotes: 0
Views: 2322
Reputation: 859
You can use get_the_category to retrieve a list of categories for a post. For example if $post
contains a post object then $categories = get_the_category( $post->ID )
will return an "Array of objects, one for each category assigned to the post".
Upvotes: 2