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 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.