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?
- Exclude pages in archives results
- Custom post type – get_day_link()
- Set URL link to featured image of custom post type
- Custom Tag Description unable to display just below and outside of the Loop
- AJAX load more for different custom post type loops
- WP_Query to select custom post type with Advanced Custom Fields (ACF) date
- On update or create post redirect to current post position in list
- I need to add endpoint for wordpress categories
- I’d like to cycle through existing posts in a post type, showing one per day ordered by title, and starting over when the last is reached
- WordPress sort search results by custom order
- Creating a custom post type, adding custom meta fields, preventing all future editability of posts of this type
- 404 error when navigating to a taxonomy page with Cyrillic, what’s wrong?
- Changing wordpress publication date to ACF date and time picker date not working
- Trouble with CPT Child 404
- Why are taxonomy terms not saving when using custom meta boxes with radio button taxonomy selectors?
- Database to page routing API?
- Use custom metabox to update automatically a post after a given date
- get_post_meta not working on publishing
- creat filter with wp_query
- Redirect 404 page with ID in slug to associated page with same ID in slug
- WordPress wrapped added a span tag to every single p tag
- How do I add a custom screen for a custom post type to be called from custom post row action?
- Action or Filter Hook for a Custom Importer
- ACF field key/value to show on taxonomy list
- Assign same parrent Page to pages AND custom post types
- WordPress custom post type permalink rewrite shows page not found
- How to set a CPT to have a page as a parent, then rewrite urls accordingly?
- Using custom field content as expression in IF statement [closed]
- Broken featured images are uploading from frontend
- Addition of custom option panel crashes Media Library & Admin Area
- Can one create multiple Custom Post Types with a for loop?
- Including metaboxes from custom post types in global search — continued
- Custom post type not showing in search result
- How to Upload CSV Data into Custom Post Type Data with Metabox programmatically
- limit value taxonomy based on previous taxonomy value wordpress
- Dynamic dropdown select values depending on other custom field value
- Allow Static Page Load Dynamic Child Pages
- Tracking changes in admin-page so user gets warning when leaving the page
- How to change wp prefix for custom post type only?
- No Permission to add new Page, Post or CPT with Admin role
- How to display custom WP menus?
- Send notifications to custom post author when custom post is edited
- What to and how to proceed with CPT to make DB small and efficient?
- Metabox not show in categories custom post type cmb2
- Categorize custom post type
- How to Create Columns with WordPress and CPTUI display page ?
- Pretty URL for custom search for custom post type
- Add post location with mile radius allowing search
- Custom post type editor with dynamic selects, one drop down populating a second second drop down not working
- Include custom post type content in search
- WordPress custom post type split into pages
- Post injections into Site Origins Page builder [closed]
- Custom Post Type + Categories
- How to add custom field to view-edit post type page
- Assign External Database Queries to Global Variables and Make Them Accessible
- Get post count for search result based on post type
- Hide a widget inside a div on specific type of post
- WordPress randomly shows 404 errors
- How to import a custom MySQL DB table into a WordPress custom post type
- Static apple-app-site-association file with wordpress
- Static page determines as home, but it is not