Tell us what you are trying to do? If the user searches in the custom search field. Is there a way to make it do an if statement to count the results, and if it only finds one, just display the single result. And if if finds multiple then show the list?
Is there any documentation that you are following? Could not find any
Is there a similar example that we can see? Sort of like Google's "I'm feeling Lucky" where it will go right to a result.
What is the link to your site?
hidden link
I can't think of a straightforward, easy way to accomplish this because Views isn't designed to allow you to redirect programmatically based on some search criteria or some results. There's no API available to do this type of operation when a custom search View filter is submitted, so it would take a significant amount of custom code to do something similar. We don't provide that type of customization here in the forums, but we do have a portal available where you can connect with independent contractors who might be able to assist: https://toolset.com/contractors
On the other hand, you can display the results of the View differently based on the number of results. In the Loop Editor, you can use conditional HTML that tests the number of items found. If it's one, then you can display that one post with a special format. It's not a redirect, but it can make the single result look different than the standard list of results.
Christian,
Ok! I have been trying workarounds. As you mentioned I could just change the layout of the "featured" item and it sent me off on a tangent, I could hide the rest via CSS. The following kind of works what I want to do. But line 3 is where the issue occurs. I need a way to have Types/Views check if there are going to be ANY "certified" listings within the loop so I could append a class at that line if so. Then I could use CSS to hide all but the first listing if the div is .listings-certified.
Would you know of anyway to do this? (What I have below at line 3 does not work, as I can see because it isn't within the loop yet so it had no way of knowing).
[wpv-layout-start]
[wpv-items-found]
<div class="listings[wpv-conditional if="( $(wpcf-certified) eq '1' )"]-certified[/wpv-conditional]">
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-conditional if="( $(wpcf--certified) eq '1' )"]
<div class="uk-panel uk-panel-box uk-margin listing certified">
<img src="<em><u>hidden link</u></em>" class="certified-banner" />
[wpv-post-link]
</div>
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-certified) eq '0' )"]
<div class="uk-panel uk-panel-box uk-margin listing">
[wpv-post-link]
</div>
[/wpv-conditional]
</wpv-loop>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]
[wpv-no-items-found]
<div class="listings none">
<div class="uk-panel uk-panel-box listing">
<p><strong>[wpml-string context="wpv-views"]At this time we currently do not have a result...[/wpml-string]</strong></p>
</div>
</div>
[/wpv-no-items-found]
[wpv-pagination][wpv-pager-current-page][wpv-pager-total-pages][wpv-pager-nav-links output="bootstrap" previous_next_links="true"][/wpv-pagination]
[wpv-layout-end]
So close and just not there yet! Grrr! 🙂
I need a way to have Types/Views check if there are going to be ANY "certified" listings within the loop so I could append a class at that line if so. Then I could use CSS to hide all but the first listing if the div is .listings-certified.
It's not possible to make inspect and analyze the result set like this within the context of a View. Let me explain one alternate way you could achieve this in Toolset.
- Clone a View identical to the View you're working with now, and add a Query Filter so that only certified posts will be found.
- In the Loop Editor of this new View, there are basically two sections - wpv-items-found and wpv-no-items-found. We will use these two sections effectively as a conditional - one to display when certified results are found and the other to display when no certified results are found.
- In the wpv-items-found block, will insert the original View shortcode here and use the limit attribute to return only 1 value. Display only the search results. Wrap the View shortcode in a div with a custom CSS class so you can target the certified result post with CSS. More information about overriding View criteria in shortcode attributes here: https://toolset.com/documentation/beyond-the-basics/re-use-the-same-view-with-different-settings/
- In the wpv-no-items-found block, insert the original View shortcode without the limit attribute override. Display only the search results. Wrap the View shortcode in a div with some other class so you can target the non-certified result post list with CSS.
Christian,
THAT WORKED PERFECTLY. I have been hitting my head against the wall for days and I thought doing the conditional was the only way to do it. Thank you so much for the solution. It works flawlessly, that way. No CSS trickery even needed now because it will only show 1 result if its certifed and if not show the list.
I knew this plugin could do it, it was just a matter of figuring out HOW to do so. Thank you again so much for your knowledge and expertise and leading us to a clear solution. 🙂