Try it like this:
if ( $query->have_posts() ) {
$categories = $category_ids = array();
while ( $query->have_posts() ) {
$query->the_post();
foreach ( ( get_the_category() ) as $category ) {
if ( ! in_array( $category->term_id, $category_ids ) ) {
$category_ids[] = $category->term_id;
$categories[] = $category;
}
}
};
}
get_the_category()
returns an array of objects. I didn’t check the in_array
documentation, but I suspect it doesn’t include object matching. $category_ids
is an array of just the ids used for comparison.
Related Posts:
- How to array only one key from another array
- Get categories names as an array to use it in theme settings
- How to get the last category name of a child category?
- Output category list inside array
- How To Pass Array To get_the_category_by_ID() and Get An Array Back?
- Finding and removing duplicates within WP Arrays
- Using multiple variables to assign categories to an array
- How to filter specific element of an array in wordpress/php?
- wp_get_archive for category returning different URLs on different but similar sites
- retrieve thumbnail from post ID of best selling product in category
- Custom HTML structure in wp_list_categories
- User Defined order on get_categories?
- Need to get specific data from array
- Categories Template Assistance
- Create an array from an array
- Error: array_map(): Argument #2
- Problems with function on function.php
- Use template for posts with a particular category grandparent
- Must Use Plugin Causing Query Error
- Display all categories including sub categories
- Custom category code not showing all posts
- Admin Options page. Save as Array
- PHP get the first post separately from array returned by wpdb->get_results()
- how can i show only the parents in owl-carousel?
- How can I add multiple ‘tax_query’ arrays via a loop?
- How to use transient in this code for related post?
- What is an equivalent of single_cat_title for getting the slug of the category?
- Pass Category Name, Description and Photo into variables to pass to jQuery
- Move category description below post list in blog
- Applying A Category to Existing Posts Where Page Title Matches Regex
- foreach loop still echoes array
- Setting default category base on theme activation
- Let Users Choose Post Categories
- Display most popular posts of category
- meaning of (array)function()
- get taxonomy thumbnail and use it as a variable in code
- Get html data with javascript to php array and store to wordpress database
- How to use array in function to get only value I want
- Warning: in_array() null given in PHP function
- Use get() method to grab all categories and output inside another method
- Only show size attributes in product box woocommerce when available
- How to access or parse key/values that have “string”
- Conditional multidimensional arrays and array_map
- Argument for if term-> have child?
- extract serialized array to use for wp-query
- get users search not working with array
- Echoing a CSS class based on category of post in a list
- How can I split my query result in 2 arrays?
- Multidimensional Array
- is user member of a group, show them categories [closed]
- How to make sure relative URL works when site is not on root domain?
- Showing Subcategory Name/Link Instead of Parent Category
- Get URI from PodPress using PHP
- WordPress custom taxonomy
- Compare current post Category in select menu
- Custom functions for string data calculations
- Get the list of post categories
- Repeat a function with 24hrs gap for n number of days
- Pagination at category doesnt work with same name of page
- Woocommerce. Get a list of products in the current category on the archive page and product page
- How to show categories and date on posts
- Problem with inserting multiple images in gallery of each WooCommerce product programmatically
- Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
- Allow two posts (from different categories) to have the same slug
- Child-Theme Category View with modified permalinks (%category% removed)
- how to separate an array into different arrays and save them into db?
- How to move a post from one category to another
- How to enable HTML tags in category description without breaking the category page
- Trying to show the category of a post in the post display
- Create category after theme setup and modify the default one
- WP post meta – for loop inside for loop
- Combine multiple queries, array_unique returns nothing
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- get_users when from meta key that has serialized values
- Displaying 3 Category Posts differently
- make an array in wordpress cusmization api?
- Code works on page-example.php by not category-example.php
- How to replace hard-coded list of custom taxonomy terms based on custom field query [closed]
- Get a list of tags present in a paged front page
- get_category display only 1
- WordPress using get_term to retreive slug not working as expected
- Add custom filter to register data in array
- Front end post submissions do not get submitted in the category
- WP_Post is not from correct array
- Auto remove empty values in array
- Display a post based on its metabox selection
- Make an array listing custom taxonomy
- Get category id for a custom category and display it in a class
- Trying to exclude posts from a category on the home page
- Adapting a php array to WordPress
- get_category only returning details for ‘uncategorized’
- Exclude a ‘portfolio’ custom category?
- Hide empty categories from get_categories
- What is this mark for “? function()” [closed]
- How to combine array info [closed]
- Remove /category/ from category (archive) page URLs (without using a plugin)
- Force array to be a string [closed]
- get_body_params() is always empty in POST request
- How to group by column a and sum column b and c in a php array
- PHP how to submit array elements to database