You can do this by checking the number of results returned by your query. Try this.
<?php
$rawfiltertag = get_post_meta( $post->ID, '_cmb_client_name', true );
$filtertag = sanitize_title( $rawfiltertag );
// wp_query arguments
$args = array (
'post_type' => 'post',
'meta_query' => array(
array(
'key' => '_cmb_client_target',
'value' => $rawfiltertag
)
)
);
// the query
$the_query = new WP_Query( $args );
if ( $the_query->post_count == 1 && $the_query->max_num_pages == 1 ) {
wp_redirect( get_permalink( $the_query->posts['0']->ID ) );
exit;
}
?>
Related Posts:
- Keep getting same permalink with WP_Query?
- Plain permalinks not working!
- How to do set post permalinks using 6 digit random unique function?
- When changing pages to posts, how do you set up 301 redirects for the page URLs?
- How to 301 redirect from url with post id to permalink with post name (slug)?
- Make assigning post to a specific category equivalent to assigning it to all categories
- 301 Redirecting posts without %postid% (just %post_name%) in permalink to ones with %post_name%_%postid%
- Permalink issue with new blog posts > getting 301 redirect
- Move first half of posts to one parent page, second half to the other page
- Setup a custom dynamic post
- Anyway to grab author name and featured image in an embeded in inital array of posts outside of loop?
- How to verify wp user password by sql query in wp? [closed]
- How to check a post exist when the permalink has post id in it?
- Removing noindex posts from wp_query
- WP Query – Posts Per Page not working in combination with category__in
- Relative redirects for posts, categories, products and product categories
- After database migration, posts not showing up in dashboard
- Ajaxify This Code
- How to filter my search in post if contains a word in title, content or excerpt?
- How to return another post than requested
- Pausing and Resuming WP_Query results
- Select only post id and meta value with WP_Query
- Lost draft under all posts and drafts
- Switch to page template when using post permalink
- Get user categories with most posts in it
- 301 (static page instead latest posts)
- Random ajax load only works with posts_per_page set to -1
- Trying to use AngularJS with WordPress without any API
- How to : pagination in 3 different custom loops on the same page
- How to handle broken links created by permalink/slug changes?
- How could I change my Permalink from blog to custom structure? [closed]
- Visting slug for ‘post’ post type shows 404 not archive?
- Any way for get_next_post() to use the actual post order instead of publish order?
- How safe is renovating $wp_query when doing WP_Query pagination in Page Template
- how do i add posts-page slug before posts slug in permalinks
- Exclude the first ‘n’ number of posts of a tag from home page?
- Query posts and return XML
- how to change the number of posts returned in a specific loop without making it global?
- Set static page/post from another blog on same network
- Query Posts With Over 1000 Views
- posts_per_page option limits the number of Gallery items
- List authors with the last post title and order by last post date
- How to order posts by meta_value and title
- Permalink doesn’t get displayed in Twitter button (Local WordPress problem?)
- Retrieve post ID from “querying” URL
- wp_query random post
- wp_posts table: safely remove unused columns to save database storage
- hiding permalink in admin if the user is subscriber
- Why does WP_Post not contain its permalink?
- Query Posts by Custom Field
- the_permalink() not working in a root installation
- Adding a blog archive with pagination using WP_Query
- How to get Tags with specific post id
- redirect to uploaded (wordpress) post after form submission from front-end
- how can i change WP main archives loop to sort by name or title
- Using wp_list_pages() after calling query_posts()
- how to get post order by post id wp_query?
- Possible to alternate between two loops
- Nested WP_Query breaking loop
- WordPress query_posts by tag doesn’t work anymore(?)
- How do I Redirect a WordPress Page?
- How to display all posts divided/ordered by post date? [closed]
- Why Query is returning empty array?
- How do I extract just the post ID of the first item in whatever WP_Query returns?
- Show post content and title in diferent divs using WP_Query using a loop
- Why are my paginated posts always returning the same results?
- Ignore latest two posts
- Is it possible to use WP_Query to only pull posts with attachments?
- Second get_permalink inside loop points to wrong URL
- Show posts by author of membership level (Paid Membership Pro)
- Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
- meta_value timestamp older than now
- Every second post different class in blog view
- Loop doesn’t exclude the specified category in home page
- Get posts with multiple categories
- Do not show children of a category
- Display posts of specific category term
- Print out last 3 blogposts
- why the same code got different results when using query_posts in functions.php and index.php
- Query posts by Author and/or by Tag
- Query for first 3 posts to change the look and feel
- How to get posts published on the latest date?
- Why posts array is empty?
- Can’t understand why sometimes a [caption] field appears
- Custom Permalink Structure for Pages & Posts
- Page and post loop same template
- Getting blog pagination to work on page set as front page
- How to group only VISIBLE posts? [closed]
- How to check in functions.php if there is data in a WP_Query?
- Shortcode with ‘year’ parameter
- search blog posts by author first name and or last name
- Show metabox value last post excerpt, title and link
- On what hook can I get the queried object’s final state?
- unique post in “$args” of wp_query display more than one post
- Display current post position in Elementor Posts widget
- custom word in custom permalink structure
- Why in my query is display two title?
- Migrate posts from category and sub-category via SQL
- How do I insert a after every 5 posts in a WordPress Loop that infinitely loads posts?
- wp_query with ajax