Skip Navigation

[Gelöst] REDIRECT TO FIRST POST IF ONLY ONE POST IS FOUND

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Redirect to the post if only one post is found with the parametric search, I'm using this code to do it within wordpress search (and is working fine)

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

Solution:
I assume you are using Views shortcode [wpv-views ...] to render the search result, the wordpress shortcode output the content, the result page HTML header has already been outputted, so it is not possible to do it with action or filter hooks, I suggest you try this:
1) Check if there is only one item in the views result:
https://toolset.com/documentation/views-shortcodes/#wpv-conditional
https://toolset.com/documentation/views-shortcodes/#wpv-found-count

2) Then output javascript to redirect the page, for example:
http://stackoverflow.com/questions/503093/how-can-i-make-a-page-redirect-using-jquery
Relevant Documentation:

This support ticket is created vor 7 Jahre, 10 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by Cesar vor 7 Jahre, 10 Monate.

Assisted by: Luo Yang.

Author
Artikel
#407881

I am trying to:
Redirect to the post if only one post is found with the parametric search
I'm using this code to do it within wordpress search (and is working fine)

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

I would really appreciate if you could help me make it work with Toolset parametric search or point me to the documentation that could let me figure out the answer.

Thanks, and huge kudos for the last update I'm now able to dig into your amazing set of plugins. Really enjoying your work, thanks for that one to 😉

Best

#408160

Dear Cesar,

I assume you are using Views shortcode [wpv-views ...] to render the search result, the wordpress shortcode output the content, the result page HTML header has already been outputted, so it is not possible to do it with action or filter hooks, I suggest you try this:

1) Check if there is only one item in the views result:
https://toolset.com/documentation/views-shortcodes/#wpv-conditional
https://toolset.com/documentation/views-shortcodes/#wpv-found-count

2) Then output javascript to redirect the page, for example:
http://stackoverflow.com/questions/503093/how-can-i-make-a-page-redirect-using-jquery

#408273

Thanks so much Luo. Amazing support, super clear answer!

I'm posting my solution just in case someone else need it, a much better approach could be implemented but this is fine for this testing

div.hide-me --> optional, hide loop result
span.post-link --> can be changed to user needs

CONDITIONAL
This goes in " Templates for this View"

<div class="hide-me">
[wpv-conditional if="( '[wpv-found-count]' eq '1' )"]
	<span class="post-link">[wpv-post-link]</span>
</div>

JAVASCRIPT
This will redirect to whatever will be in .post-link <a href

jQuery(this).ready(function(redirect){
    var href = jQuery( "span.post-link" ).find( "a" ).attr('href');
	window.location.replace(href);
});

CSS
Hide .post-link in search page

.hide-me {display:none;}

Thanks again for your support, have a nice day

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.