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?
- Block Editor – WordPress 6.1 – CPT Archive Issue – While the title changes in the loop, all records display the same data
- $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
- Custom WordPress Archive and query dont filter by categories
- When should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- WP_Query by just the id?
- Meta_query compare operator explanation
- WP_Query with “post_title LIKE ‘something%'”?
- wp query to get child pages of current page
- How to get an array of post data from wp_query result?
- How to get post id of static front page?
- meta_query with meta values as serialize arrays
- How to only display posts whose meta_value field is not empty?
- Get post ids from WP_Query?
- Pagination when using wp_query?
- Exclude post ID from wp_query
- How to print the excuted sql right after its execution
- How to extend WP_Query to include custom table in query?
- How to store and receive variables in WP sessions?
- Should I use Pre Get Posts or WP_Query
- How can i retrieve default post per page value? from settings->reading. And total number of posts?
- Nested meta_query with multiple relation keys
- Is it necessary to use wp_reset_query() in a WP_Query call?
- Can I force WP_Query to return no results?
- Can wp_query return posts meta in a single request?
- Using pre_get_posts with WP_Query
- Find out total number of pages in global query on archive page?
- WP_Query + random
- Resetting post data to previous loop in nested loops
- order by numeric value for meta value
- numberposts? showposts? posts_per_page?
- $GLOBALS[‘wp_the_query’] vs global $wp_query
- Broken? WP_Query and “attachment” as a post type
- Some doubts about how the main query and the custom query works in this custom theme?
- Order by multiple meta key and meta value [closed]
- Post_count only shows the number of results per page
- Page template query with WP_Query
- How to use a custom post type archive as front page?
- meta_query ‘compare’ => ‘IN’ not working
- Query posts only with featured image
- Custom Post Type Archives by Year & Month?
- Get custom post type slug for an archive page
- Wp get all the sub pages of the parent using wp query
- What is appropriate flow for custom data from URL in WP_Query?
- How to display page content in a page template?
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- How to merge two queries together
- Posts with at least 3 tags of a list of tags
- Get post count of current loop when using multiple queries on one page
- why ignore_sticky_posts in sticky post query
- Wp_redirect and sending variables
- Multiple relationship for multiple tax_query in WP_Query
- How do I get posts that have a thumbnail in WP_Query?
- Multiple WP_Query loops with Pagination
- How do I query for posts by partial meta key?
- Pagination with custom SQL query
- Return only Count from a wp_query request?
- Using a custom WP_Query with get_template_part loop
- Use REGEXP in WP_Query meta_query key
- Why is the loop not empty on some 404s?
- WP_Query orderby post__in remains ineffective in the Loop [closed]