Opened 2 years ago
Last modified 12 months ago
#17592 new defect (bug)
'exclude_from_search' excludes from non-search queries
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Query | Version: | 3.1.2 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | nacin, msykes@…, scott@…, aaroncampbell, ruud@… |
Description
Any WP_Query using 'post_type' = 'all' will only return post types where 'exclude_from_search' is FALSE. I would expect 'post_type' = 'all' to include all post types, *except* when initiated via a front-end search.
Recommend adding an is_search() check when excluding post types from a 'post_type' = 'all' query.
Attachments (3)
Change History (18)
comment:2
follow-up:
↓ 4
ocean90
— 2 years ago
- Keywords needs-testing removed
Also running in this problem today, because when exclude_from_search is true also the archive page is empty.
comment:4
in reply to:
↑ 2
kawauso
— 2 years ago
Replying to ocean90:
Also running in this problem today, because when exclude_from_search is true also the archive page is empty.
Just curious why you used the is_search() function shorthand over the method of the current query?
comment:6
mitchoyoshitaka
— 20 months ago
Kawauso, looks good to me. Note that your patch doesn't apply cleanly after #18364.
comment:7
follow-ups:
↓ 8
↓ 12
ryan
— 20 months ago
- Milestone changed from 3.3 to Future Release
Introduced in r13052 and as far as I recall was done on purpose. Regardless, I don't think we can change the meaning of 'any' at this point. We probably need a way to ask for all public types and all types.
comment:8
in reply to:
↑ 7
duck_
— 19 months ago
Replying to ryan:
Introduced in r13052 and as far as I recall was done on purpose.
And r11998 for the post_type query variable.
I ran across this whilst testing a term archive for a custom taxonomy registered to a custom post type with exclude_from_search == false (public => false, but publicly_queryable => true).
comment:9
netweblogic
— 18 months ago
- Cc msykes@… added
Whilst working on my plugin, I had to make a workaround. This seems to work:
function my_cp_parse_query( ){
global $wp_query;
//Search Query Filtering
if( !empty($wp_query->query_vars['s']) && $include_in_search ){
$wp_query->query_vars['post_type'] = array_diff( get_post_types(array('exclude_from_search' => false)), array('my_custom_post'));
}
}
add_action('parse_query','my_cp_parse_query');
It just checks to see if a search is being made, if so, get all the searchable post types and remove yours from the list, supplying that array to post_type query var. Hope that helps someone!
comment:10
l3rady
— 16 months ago
- Cc scott@… added
comment:11
l3rady
— 16 months ago
I just came across this and it took me hours for me to work it out.
I had a CPT called locations and had 'exclude_from_search' => true. I then created a custom taxonomy for this CPT called ltypes. I then created taxonomy-ltypes.php to list all posts that have the chosen taxonomy, only to be greeted with no posts found...
comment:12
in reply to:
↑ 7
aaroncampbell
— 15 months ago
Replying to ryan:
Introduced in r13052 and as far as I recall was done on purpose. Regardless, I don't think we can change the meaning of 'any' at this point. We probably need a way to ask for all public types and all types.
I think it's really common to have a post type you DON'T want in searches but you DO want taxonomy pages for. Maybe in query.php where we check if ($this->is_tax) and set $post_type = 'any'; we could instead set it to whatever post types the taxonomies support? Or possibly set it to 'all' and add another condition that uses get_post_types() instead of get_post_types( array('exclude_from_search' => false) );
comment:13
aaroncampbell
— 15 months ago
- Cc aaroncampbell added
By the way, the workaround I used was to show 'news' posts on the 'news_item_cat' taxonomy pages. I did it like this:
function pd_only_query_tickers( $q ) {
if ( is_tax( 'news_item_cat' ) )
$q->set( 'post_type', 'news' );
}
add_filter( 'pre_get_posts', 'pd_only_query_tickers' );
comment:14
ruud@…
— 12 months ago
- Cc ruud@… added
comment:15
ruud@…
— 12 months ago
Ran into this also. Tried to use the exclude_from_search on a cpt but found out archives/categories are empty. Going to try the workarounds.
Apply exclude_from_search only when is_search() for post_type & post_status