WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 6 months ago

#29418 new defect (bug)

Taxonomy archive query not including all of its post types.

Reported by: msaggiorato Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.2
Component: Taxonomy Keywords: needs-patch
Focuses: Cc:
PR Number:

Description

I've noticed this in a project i'm working on.

I did some digging in wp-includes/query.php file, and found that this is the issue.

In 3.9.2 files, in line 2501 there's this

foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {

Not sure if this is intended or not, maybe yes, but the query var name is missleading in this case, which is also excluding the post type from its taxonomies archives too.

In any case, if this is to be fixed, and in fact is not supposed to be here, just changing the line for this should work, as we should expect.

foreach ( get_post_types( array() ) as $pt ) {

Hope this helps, thanks.

Change History (9)

#1 @SergeyBiryukov
5 years ago

  • Summary changed from Taxonomy archive query not incluiding all of its post types. to Taxonomy archive query not including all of its post types.

#2 @boonebgorges
5 years ago

  • Keywords 2nd-opinion added
  • Milestone changed from Awaiting Review to Future Release

The behavior was introduced in [21855]. See #21290.

exclude_from_search is, as you'd guess, intended to exclude items from showing up in search results. However, it's also used in a number of places through WP_Query in a more general way to prevent posts from a given post type to appear in results, and this is one of those instances. The naming convention here is quite poor, and it points toward the need for a different parameter. (I would've thought this was what 'publicly_queryable' was for, but that's not actually used in WP_Query at all.)

In any case, unfortunately I don't think we can pull up all post types here as you suggest. I think that'll cause problems for sites that are depending on the current behavior. Better would be to introduce a new param that inherits its value from 'public' or even from 'exclude_from_search' and means something like "show in archives".

#3 @msaggiorato
5 years ago

I like the "show in archives" approach, could be a good "fix" for this.

This ticket was mentioned in Slack in #core by ocean90. View the logs.


5 years ago

#6 @helgatheviking
4 years ago

However, it's also used in a number of places through WP_Query in a more general way to prevent posts from a given post type to appear in results, and this is one of those instances.

Seems like a mistake that exclude_from_search would mean anything other than excluding from search. Couldn't the other places that use it be converted to publically_queryable, since that sounds like it means the CPT should show up in any public queries? or failing that a new parameter... show_in_archives seems alright, if redundant.

After spending the whole morning where the heck my CPTs were in my taxonomy archive, I'd take anything.

#7 @boonebgorges
4 years ago

  • Keywords needs-patch added; 2nd-opinion removed

#8 @knutsp
4 years ago

I agree with @helgatheviking. This is a mess, and it has to change.

#9 @DoodleDogCody
4 years ago

One thing not mentioned in a few of these "exclude_from_search" templates that I have stumbled upon is that if we set it to true and then try a custom loop for the CPT with a taxonomy term in a tax query. We can only get the 1st page of results. So using something like the following only returns results for the first page, while the rest of the pages return a 404. So if at the end of our loop we have some pagination, the pagination knows how many pages there are but when going to those pages it just stops.

I agree with the previous posters that something here really needs to change. 'exclude_from_search' should do just that and exclude_from_search while another parameter 'publically_queryable' or some new parameter is used here. Not wanting search results to return this CPT but wanting to have taxonomy archives pages seems like something that might happen a lot and all the answers to accomplishing this seem like big workarounds.

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $taxonomy = get_query_var( 'taxonomy' );
    $term = get_query_var( 'term' );

    $args = array(
      'post_type' 		=> 'ddc-products',
      'posts_per_page' 	=> 5,
      'paged'				=> $paged,
      'tax_query' => array(
        array(
          'taxonomy' => $taxonomy,
          'field'    => 'slug',
          'terms'    => $term,
        ),
      ),
    );
Note: See TracTickets for help on using tickets.