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?
- Display custom taxonomy as dropdown list
- WordPress is executing URL in code when called via wp_mail()
- Get all taxonomies for all post types
- How to redirect all pages of a custom post type [duplicate]
- Row actions not showing? Why?
- Displaying custom post type on front page
- Make custom post type display as a page
- wp_list_pages doesn’t work in hierarchical custom post type
- Append taxonomy terms as class names in markup?
- How to exclude a widget from page 1 of a paginated post
- Search tags in CPTs
- add_meta_box Callback not being called
- Custom Post Type .current-menu-item not applying on Custom Post Type Archive Page
- How do you output custom code between posts in the loop?
- Show ACF field from custom taxonomy and display on the single template
- How to allow “Add New” capability of CPT when links to its UI are placed as a submenu?
- Change message given when deleting post from custom post type
- How to search CPT’s by meta query from the admin dashboard?
- Set meta_query only for specific post type
- Display children and grandchildren of a custom post type
- Building an Advanced Search (text, tags, category, custom fields) – Getting the wrong SQL query
- Creating search filter through plugin
- Best practice for adding posts in bulk
- Customize Search Results for Custom Post Type
- wordpress custom post type remove duplicate menu item
- Using WPAlchemy metabox values in another metabox
- multiple search forms and search results
- error in Advanced Search Form for Custom Post Types in WordPress array_key_exists() expects parameter 2to be array, string given
- is_page_template not working as expected
- List terms from Custom Taxonomy
- Hide Meta Boxes for Non-Admins
- Multiple portfolios with one custom post type?
- Search Custom Post Type Custom Fields
- Is it possible to use pages to apply static content to each taxonomy level?
- Making pages also serve as taxonomies? Or give full pages to taxonomies?
- Custom Post Type not appearing in website search
- How to get images from EDD post?
- WP_Query search posts by custom post type and custom taxonomy
- Share parent path between Custom Post Types and Pages?
- Issue to get wp_get_attachment_image with cmb2
- How to have the right design for a custom post type without accessing themes
- Dynamic Custom Post Type Plugin
- Order WordPress Custom Taxonomy Pages & Pagination Not Working
- I am having a problem with form updating/editing WordPress post on the frontend
- How to filter out post type meta?
- ‘Pages’ widget alternative for custom post types?
- Best action hook to create custom post and assign taxonomy terms to it on plugin activation?
- How to make in WordPress admin panel sortable column for the custom field, that count the number of page impressions?
- Custom Blog Post Listing in Genesis Sample Child Theme
- Custom Fields vs Separate Table
- Add custom post type archives to search results?
- Display custom field of specific post where post title matches variable
- Conditional to modify query results
- CPT Meta Searching
- CPT Search Form with Taxonomy filter & Or
- Loop through posts of a custom-post-type (event) and create .ics (iCal) file?
- Check if admin is editing page or custom post type
- remove child post from custom post type archive
- Pull in custom content types into page template
- Custom Post Type | Fatal Error on register_post_type()
- Output custom post shortcode. Help spot the error.