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
- Query posts only with featured image
- Get custom post type slug for an archive page
- get_query_var function not working at all
- get_query_var( ‘paged’ ) not working outside of homepage
- Order by Category and Post in WP custom Query
- Execute a large WP_Query with many “AND” Meta_Queries?
- How to know which one is the main query?
- WP_Query can’t exclude more than 1 author?
- How to order posts by custom WP role?
- Generate a tabbed submenu — from taxonomy term or submenu item — with sample content
- posts2posts query using connected_items array issue [closed]
- Explanation of WP_Query
- Undefined property: WP_Query::$post
- Excluding Sticky Posts from The Loop and from WP_Query() in WordPress?
- Create A Loop With A Variable Number of Posts For Each Bootstrap Row?
- Set Transient does nothing
- Get amount of CPT with a certain custom field value
- query offset countdown by 2 each day?
- How do I make a meta_query OR relation work? [closed]
- How to use wp_query to post count by search term > group post count by date > convert to json. (ex. result 12-21-2020 – 343)
- Filter posts in category archive page by year using a dropdown
- Woocommerce – Changing the order of the upsell products [closed]
- Custom Query by Author field and meta
- Include post content of linked posts in search
- Query_posts works when appending via AJAX call wp_query doesn’t?
- wp_trim_words strips dashicons
- Save (and exclude) posts from loop to use in another loop on page
- pass parameters through variable to WP_QUERY
- Can’t order the query result as given posts id’s array
- Trouble Making WP_Query paged
- Sort posts using multiple custom fields and menu_order in single query?
- Generate custom output on URL with directory
- prevent get_comments() from returning comments not in comment__in
- WP Query should show No Posts when tax_query $args taxonomies don’t have associated posts
- How to make WPQuery custom post type work in Twig / Timber
- Display posts from multiple value in meta separated by comma
- WooCommerce. How To Exclude Subcategory Products From Category Listing Page
- IF ( ! $loop->have_posts() ) condition doesn’t work – WP_Query
- get_posts return only first result
- Getting posts by custom field value
- Creating an archive page or simple template to list all values of a custom field of specific post type listing
- wordpress multiple meta value query is not working
- trying to change from query_post to WP_Query
- Strange results from WP_Query
- update_post_meta performance in a loop woocommerce
- WordPress For Loop Prints Unwanted Extra Paragraph Element
- return child post if available otherwise parent post
- URL Rewrite and Archive Template Files – Post Type vs. Taxonomy
- WP_Query doesn’t work when url parameter is added
- why ignore_sticky_posts in sticky post query
- Woocommerce orderby rand with tax_query not random
- Select Unique Posts for a List of Tags
- WP_Query always returning the last custom post
- why is this query returning entire blog-posts
- Pagination error WordPress multiple loop
- Multiple wp_query loops showing first 5 posts on all pages instead of older posts[Resolved]
- SQL returned by Wp_Query has wp_posts.ID = -1
- Prevent WordPress loop from displaying similar post titles
- Limit paginated result set to a maximum number of posts