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?
- Inconsistent temporary 404s on whole install | PHP error with post-template.php
- How to make a WP_Query search with custom post types?
- Load custom posts with same tag as page
- Returning search results by relevance, including Custom Post Types
- How to replicate some of Drupal Views functionality in WordPress?
- Exclude from search all custom posts which are NOT in a taxonomy term
- How can I add dropdown widget/box to admin post page?
- Meta Query “IN” doesn’t work with ACF checkbox filter
- How Do I Use WP_Query to Run This Database Query as Search Result?
- Custom Post Types: pretty search URLs and has_archive
- Filter posts with ajax form and checkboxes
- Get custom post type REST API not working
- Used with meta_query in query_posts works slowly
- Get all taxonomies for all post types
- How to exclude a widget from page 1 of a paginated post
- wordpress custom post type remove duplicate menu item
- Custom Post Type not appearing in website search
- WP_Query search posts by custom post type and custom taxonomy
- Loop through posts of a custom-post-type (event) and create .ics (iCal) file?
- Keyword search limited to specific post type filtered by multiple custom taxonomies?
- custom posts on different page
- Custom search for custom post meta with pre_get_posts interferes with WP search
- get_posts of Custom Post Type AND Custom Taxonomy
- Update menu when saving settings
- Get parent categories of custom post type
- Custom Tag Description unable to display just below and outside of the Loop
- WordPress sort search results by custom order
- Can one create multiple Custom Post Types with a for loop?
- Pretty URL for custom search for custom post type
- Add post location with mile radius allowing search
- How to add custom field to view-edit post type page
- custom post type and non-standard gallery
- How to change permalink to include custom post type
- How to Add multiple instance of meta box to custom post type
- Why does not this wp_redirect work in is_single()?
- Add first letter of titles to array, then compare arrays
- Add categories to a page with out using Custom Post Types
- widget_posts_args not using the number of posts in widget
- Create a simple Testimonial page
- List latest post out of 2 custom post types
- How to get the custom post data using post id in WordPress?
- Problem with checked box on wp car manager plugin
- WP_Query – How To Query Only Custom Posts With No Children of Their Own?
- programmatically generated custom post type is created more than once
- Create posts inside CPT post
- Taxonomies are not showing in the category dropdown
- Show Post columns to specific users on condition
- Custom Taxonomy – fields
- Print post category name from a custom post type wordpress
- CPT post doesnt shows tags and category on backend admin page
- Create Inclusions and exclusions
- Search form to find custom meta box generated data
- Custom Info Box at end of posts?
- How to display data with pagaination on backend?
- 404 error Custom search results page for custom post types with friendly URL
- Show Custom Post Type taxonomy term that matches custom field
- Excerpt for each post type and for is_front_page
- is_singular won’t call my functions?
- How to add post type in masonry posts elementory widget
- Remove plugin’s custom post type archive page and single pages [closed]
- Comments are not working on Custom Post Type