To include the attachment
(media) post type in search results, use the following code snippet
function attachment_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'attachment' ) );
$query->set( 'post_status', array( 'publish', 'inherit' ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'attachment_search' );
However, this does not take care of how the search results are displayed but that’s a discussion for another thread.
This should’ve worked, but if you want to add pages as well, then replace
$query->set( 'post_type', array( 'post', 'attachment' ) );
with
$query->set( 'post_type', array( 'post', 'page', 'attachment' ) );
Related Posts:
- Include images from pages in wp search.php results in default wp search
- Searching multiple custom post types and pages
- Development of a WordPress Search Plugin – Best Practices
- Search that will look in custom field, post title and post content
- Separating Search Results By Post Type
- List events by month
- How to append element after thumbnail
- GravityForm: Populate Dropdown with custom post type [closed]
- Can’t get order_by meta_value_num to work properly
- WooCommerce product search titles only
- Page vs Custom Post Types Differences/Issues
- If post has custom field then display css-class
- Replace text in post from cvs
- Make parts of your wordpress website completely built with data from external APIs?
- search suggest – filter post type
- Make a custom_post translatable
- How To Post WordPress Custom Post Types to Twitter via IFTTT
- Pagination hitting 404 page on /page/4
- Filter content in shortcode
- How to set the mainpage of a custom post type?
- Validate Custom Post Type fields
- Accessing a protected property of a post
- Sets post_parent in custom post type posts automatically based on cpt->page name
- Is it possible to get the specific content on the search page?
- In the php, how can I have a category of a custom post type display?
- How to use posts as tabs in a custom page?
- Accessing download link from the loop with WP Download Manager Pro
- Set a template on a custom post in the plugin
- Do not show child pages within a file page
- Disable Sidebar on certain pages
- Filter Custom Post Type Posts by Taxonomy
- Custom search for a custom post type in WordPress
- How to display all custom fields associated with a post type – IN THE ADMIN AREA?
- WP_Query return highest number only
- Show custom taxonomy not in submenu
- Loop carousel slider in wordpress
- How to Fix WordPress 500 Internal server error due to custom post type
- Custom Search not working
- How to hide home title on pages and posts?
- How can I remove the “Add New” button in my custom post type?
- How can I change the admin search posts fields?
- Get Custom Taxonomy ID within loop
- Rewriting ‘rewrite’ slug for custom post type used by plugin
- TV Show database – Best way for structuring it?
- Search everything (posts, pages, tags, cpt, meta)
- Search Post Title Only [duplicate]
- Custom Post Type Name Causing Problem
- How to get images from EDD post?
- How to search map address in custom post type?
- Shortcode insertion in tab
- Custom Search for Drafts in Custom Post Type
- Create custom PAGE with register_post_type
- How i can add ‘N’ page of ‘N’ pages under posts loop?
- What’s the difference between same wp functions get_posts(); functions in different form?
- Checking if Post Title is Unique as Loop Criteria
- Custom post type search using $_SESSION and pre_get_posts
- On update or create post redirect to current post position in list
- Assign same parrent Page to pages AND custom post types
- Custom post type not showing in search result
- Search by tag name and category
- Different results between permalink and query var search
- How to add multiple images to a custom post type without plugin [closed]
- Why is get_pages() returning a boolean?
- What content types are shown at a search page?
- How to create multiple editor?
- New “Custom Types” item in admin menu. Is this a plugin or a new wordpress feature?
- Conflict between wp_list_pages and get_posts – list pages not displaying
- Assigning a category to a custom post type in WordPress
- I need to add a filter to prepend the term ‘National – ‘ to the post title if the post is tagged to multiple states
- Show message when query has no posts
- Woocommerce Backend Search by Title and SKU
- Custom Search | check multiple meta_value for search value
- get_terms() returns Trying to get property of non-object error for custom taxonomy
- Custom pages missing on “front page” setting
- Send post changes to set email addresses (not users or subscribers)
- Foreach loop returning more than one item when querying taxonomy
- Limit search field to just search a custom post type with custom fields
- Compare Two Custom Post Types Using The Same Custom Taxonomy
- Custom post type page with parameter
- Custom fields array to display it monthly
- How to show the post which checkbox is not selected
- Check that a slug is present in the get_terms request
- open custom post type in popup window instead of page
- Get category name of custom post type
- pagination problem listing custom post types of taxonomy in wordpress
- Search Function not searching correct custom_post_type even though post_type is defined in search args
- Python in WordPress – A “Hello World” implementation
- Custom Post By Category
- Display a list of sub-pages of Custom Post Type Parent Page
- test if something is a post or a taxonomy
- Create unordered list from custom field type entires separated by a comma
- How to create groups like that in buddypress?
- Show tags of custom post types in WordPress
- get_post_types doesn’t work in plugin
- wordpress form processing to custom post type not working
- Is it possible to make one of two custom fields in Custom Post Type UI Required but not the other?
- Display Custom Post Type on Toolset Types Plugin
- Retrieve a post with its ACF repeater fields in wordpress
- Issue displaying multiple TinyMCE editors with WPAlchemy
- Limit a search box by CPT + taxonomies, in only one page