I just figured this one out by modifying the first foreach statement.
$post_id = get_the_ID();
$cat_ids = array();
$categories = get_the_category();
if ( $categories && !is_wp_error( $categories ) ) {
foreach( $categories as $category ) {
if( 0 == $category->parent )
{
continue;
}else{
array_push( $cat_ids, $category->term_id );
}
$current_post_type = get_post_type( $post_id );
$args = array(
'category__in' => $cat_ids,
'post_type' => $current_post_type,
'posts_per_page' => '5',
'post__not_in' => array( $post_id )
);
Related Posts:
- Exclude or Include category ids in WP_Query
- How to query only for products with status “in stock” in WooCommerce? [closed]
- WP_Query to show post from a category OR custom field
- get_posts with multiple categories
- ajax category filter
- Display recent posts from the same category as current post in sidebar
- Counting number of posts with Category B in Category A
- Use Transient API to cache queries for all posts in all categories?
- Sorting Posts by custom field
- WP Query with multiple categories – passing an array works?
- wp_query display posts from same category of the post
- How to show only one post for each categories of taxonomy of custom post that contains a specific custom field
- WP_query category__in not working, only pulls from first category
- WP_Query order by date in meta_value
- get complex results set according to category structure
- Searching through different categories on different pages code is not working
- How To Remove/hide some specific categories from two different categories widget from sidebar
- How to select posts from one category but exclude posts in another category?
- Get Category Archive Template Name Dynamically
- How can you get first post, last post and post count in a category?
- How to get count of posts assigned to given category?
- How to show subcategories using loop?
- List of the years with posts presented
- Show all post for a given category
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- Category applied to pages, creates multiple breadcrumb entries after a search query (On the translated site)
- category query for pages not working
- Can we return all category (not post) with Custom Query Filter? [closed]
- Query posts intersecting tags and categories
- Query posts from category A, and from either category B or C
- Combining categories (Query posts with multiple taxonomy terms)
- Filtering out child category posts from parent category archive not working
- Exclude Category filter from Portfolio section
- Inserting HTML to close and open divs in WP_Query loops
- Limiting number of related posts
- Category ‘pad_counts’ & ‘parent’ conflict
- Finding WordPress Posts assigned to multiple categories
- Include posts from some categories while excluding from others
- wp_query is showing posts from other categories
- WP_Query Taxonomy categories filtering
- Hide products in uncategorized category from search results
- How to load a script code only in posts?
- How to echo woocommerce category name
- Slider won’t work with custom query
- WP_Query with one category in args shows other categories
- How to exclude a category name from showing?
- Loop categories by recent post
- How order posts from category by date and comment count?
- Get the child category ID of current category
- Related Posts Excluding Certain Categories
- Post incorrectly excluded when using “category__in”?
- Related posts by current posts child category
- Filter sub-category from checkbox form
- `offset` WP_Query argument dont work via `pre_get_posts`
- Use get_cat_ID to retreive multiple category IDs
- Function the_posts_pagination() not compatible with WP_Query arguments
- Filter products on category AND tag
- adding pagination to a foreach loop in wordpress
- query hook parse_tax_query function takes no effect
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- How to use the Term Object from a custom select field in a query
- 3 posts from each existing category on one page
- How to display the category dropdown auto search list when key press?
- Categories In English version showing not canonical URL, instead shows query search result
- What code to use in an array to call the current sub-category?
- Get categories within specific term
- How to show specify category template for both parent and child category
- Pagination for Category does not work
- Exclude parent categories from recent posts list
- Query post by Category and custom file (ACF)
- Check the product in the cart from which category is and show message
- WP_Query showing all posts, except from category X, unless it’s also in Y
- Single query for multiple categories
- Display 3 levels of categories on page
- Filtering ‘Featured’ posts from a batch of category IDs
- Multiple values in WP_Query : category__and
- How to exclude posts by ID within a category/archive loop
- I have 3 categories, i want to display on a loop the last 3 of every category
- Adding Category in WP_Query Not Working
- display all posts from category with and without terms in chronological order
- Help displaying related categories
- Pull posts from all categories if quantity is not met?
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- Category Archive not working for pages
- Show full category tree for a year with all post titles?
- Retrieving category pages from subcategory returns empty sets
- Display Count of posts
- Display post list within category list sorted by name [duplicate]
- Create multiple sections for all categories and then queries all the posts for each of those categories
- query_posts problem – need help
- Related posts with WP_Query
- Main loop querying current template’s info only in custom category archive pages, not my posts
- query_vars category_name only display one catgory out of multiple categories
- List categories using WP_Query
- I need to get all categories from a WP_Query
- Advanced Search – Is this possible?
- How to get post taxonomy url and name in wp_query
- Pagination only showed when no category is set in wp_query
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Excluding a category from frontpage but not from WP_Query