Opened 10 years ago
Closed 10 years ago
#33827 closed defect (bug) (invalid)
WP Query: Custom Post Types and Categories
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.3 |
| Component: | Query | Keywords: | |
| Focuses: | Cc: |
Description
Hi,
It seems that you cannot use a custom post type's category as a filter during a WP Query, see here:
<?php
$the_query = new WP_Query('cat=4&taxonomy=product_categories&posts_per_page=-1&post_type=products&status=publish' );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<?php
$queried_objects = get_the_terms($post->ID, 'product_category');
foreach ( $queried_objects as $queried_object ) {
$current_term = $queried_object->term_id;
}
?>
Whenever a category ID is placed in to the query, no results will be returned, even though there are items in there.
Change History (3)
#2
@
10 years ago
I think we need to get this changed, so that, if it is a custom post type, the cat= will look for the categories within that post type.
This would majorly break backwards compatibility.
The docs are very clear that cat is a category (built-in taxonomy category) parameter. Just like that, tag is a tag (built-in taxonomy post_tag) parameter.
Custom post types usually use custom taxonomies. Category is just one taxonomy, terms is another. Developers are encouraged to use tax_query for queries involving custom taxonomies rather than mis-using cat and tag for something they aren't intended for. Even if that means more typing.
#3
@
10 years ago
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
As noted by swissspidy, this seems to be a misunderstanding of terminology. 'cat' is short for 'category'. 'category' is a specific taxonomy. When you register a custom taxonomy 'product_category', this is *not* the same as a WP 'category'. The two are siblings (they're both types of taxonomies); 'product_category' is not a type of category.
I do recommend full WP_Tax_Query syntax in your code, for clarity. However, you can also use the following shorthand:
'product_category=foo,bar&...'
where 'foo' and 'bar' are the slugs of terms in your 'product_categories' taxonomy.
Thanks for the ticket!
Seems that, since custom post types were added, this area of WordPress has been left a little untouched.
Allow me to explain, after a bit of research (and discussing with some guys at work), the issue lies around WordPress v3.
Custom Post Types don't use the normal default category system, they use, essentially, a custom category, now, expected behaviour would be that the "cat=" parameter would work, because the system is still only looking for one type of category.
However - as this area has been left untouched, that's not the case, and instead we have to do something much longer, like:
I think we need to get this changed, so that, if it is a custom post type, the cat= will look for the categories within that post type.