Here is a rough filter:
add_filter(
'template_include',
function($template) {
global $wp_query;
if (1 == $wp_query->found_posts) {
global $wp_query;
$type = $wp_query->get('post_type') ?: false;
$template_type = $type ? 'single-' . $type. '.php' : 'single.php';
if ( locate_template($template_type) ) {
return locate_template($template_type);
} elseif ( $type && locate_template('single.php') ) {
return locate_template('single.php');
}
}
return $template;
}
);
You will need to alter it so that it deals with custom (Edit by G.M.)single-{*}.php
templates gracefully.
I may edit the code a little later but I thought I’d get you started.
Related Posts:
- In Product Category archives how to show Posts having same/similar prod_cat slug structure?
- $wp_query initiation?
- Custom archive page based on array of categories and tags
- Ordering Posts Type A by Custom Fields of related Post Type B
- is_main_query() not working for WP REST API
- List of the years with posts presented
- From where is archive.php getting its posts?
- Display posts from only one post form in custom query and exclude in main query
- Remove category from query (show all posts in archive.php) pre_get_posts()
- How to get all product attributes for filters in WooCommerce?
- Two loops on archive page
- How to order posts by title after they have already been sorted by category
- Filtering out child category posts from parent category archive not working
- How to display an other custom post type in a different custom post type’s archive?
- Custom post type archive pagination query issue
- Custom Post Type Query issue
- Display if author page is author page of current user
- Filtering posts by archive showing all years
- WP_Query – How to get all posts of specific days of week by custom field date?
- Display all posts in a custom post type, grouped by a custom taxonomy. How to sort the posts alphabetically and the terms by ID?
- Creating attachments archive in tags and categories
- Post archive for certain post format
- Creating ‘posts page’ loop based on the page itself
- get_children() Archive Template
- wp-query and wp_get_post_terms on custom taxonomy archive page
- In a WP_Query can I force the results’ is_singular() to be set to false?
- Pagination not working on archive.php template?
- Redirect logged in users from a custom post type archive page if they don’t have a specific role
- How do I stop the same post showing multiple times in a archive?
- Tell wordpress to show a single page instead of an archive page
- Custom Post Type WP_Query with filters and search
- WordPress custom query by archive title
- Show full category tree for a year with all post titles?
- How to allow Readers to Select Post Order?
- Get X posts for all post types on Taxonomy term archive
- Custom post type archive organized by category
- How to add archives (with pagination) to page template?
- Main loop querying current template’s info only in custom category archive pages, not my posts
- query only direct child and sub-terms of a current term archive
- the_excerpt() not working in custom archive
- Exclude a category if post is only in that category using wp_Query
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Use of caller_ get_ posts
- Tax_query terms ID’s using variable
- Query certain amount of posts from multiple dates
- Order posts by tags count?
- Querying Multiple Custom Taxonomy Terms
- Unable to combine “search value” and “tax_query” using WP_Query
- How to extend tag and category “Related Posts” query to custom post_type if the first 2 terms have no posts
- WP_Query search for whole words
- WP_QUERY returns empty set when fired from a WP-CRON scheduled event
- WP_Query, tax_query and term_meta: How to?
- Pull post meta with post_query?
- How to use WP_Query to display many posts?
- Complex WP_Query (two post types and multiple operators)
- Five posts from a category in footer
- Return only post(s) which have post_excerpt
- URL of a custom post type’s post format archive?
- Front page with registered query params in URL shows blog archive instead of front page
- Order by empty custom field
- Warning: Invalid argument supplied for foreach(). when creating a WP_query
- New WP_Query loop in admin causes problems
- How to sort a custom wordpress query by combination of meta values?
- Get author if post has multiple authors
- What happens to the default query when I use WP_Query?
- Modifing archive query affects show post count function
- For what queries is conditional tags informative?
- posts archive page – closing WP_Query loop correctly [closed]
- JSON – Create rest api endpoint for Advanced Custom Fields
- Is it possible (and how to) query single Gutenberg blocks?
- Get meta_value of a specific meta_key from all posts belonging to a specific custom type
- Nested array issue in meta_query
- Query by key or author
- Why doesn’t my WP Meta Query return any results?
- How to build the WP_Query using the code?
- How go give $value to wp_query
- How to make the ‘request’ filter work?
- Related categories order posts by category
- Finding post ID dynamically on click
- paginate_links method doesn’t show second page of my custom wp_query
- Wp_Query Post per page not working?
- Order Element By Custom Field in WordPress
- Display All Top Child Categories / Taxonomy
- how to get custom attachment url?
- ACF Post Object meta-query by title not ID
- Case insensitive ORDERBY in wpquery
- Sorting with meta_query and multiple, optional meta keys
- getting posts by tags
- WP_Query & Duplicate entries
- Query child posts with tax query on parents
- How to break up output of posts for different terms on same page?
- Calculating efficiently on large amount of data generated by wp_query
- Include post_status check within $wpdb query
- Return on a quest all these meta_value
- Complicated WP_Query
- Custom query to show posts
- Taxonomy related query not working
- Multiple Orderby is not working right
- How to get all post_id and meta_value using meta_key in wp_postmeta table
- List categories using WP_Query