Skip Navigation

[Resolved] Question about custom search / filter code

This support ticket is created 5 years, 2 months ago. 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.

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 12 replies, has 2 voices.

Last updated by josephC-5 5 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1366999

Hi there,

I had a question about the storage location of the custom search / filter code. I have two views that have the exact same custom search / filter code. I'd prefer to consolidate the code for ease of maintenance and store it in one place. I tried to move the code to a content template, but that didn't seem to work. My question is, can the custom search / filter code live in one place and appear in multiple places, like content templates and views can normally do? Or does that code have to appear in the "Search and Pagination" section of a given view?

Thanks!

Saul

#1367095

Dear Saul.

Please elaborate the question with more details:
How do you setup the "storage location of the custom search / filter code"?

If they are custom PHP/HTML codes, you can try to setup them as a custom shortcode:
https://codex.wordpress.org/Function_Reference/add_shortcode

Then use the same shortcode in both views.

#1367683

Luo,

I'm sorry if my question was unclear. Perhaps I can rephrase it: can I put the custom search / filter code that usually lives in the "Search and Pagination" section of a View in a Content Template? What I am trying to accomplish is to use the exact same filter with multiple views.

Does that make sense?

Saul

#1368335

It isn't clear, I am not sure how do you setup the "custom search / filter code", can you copy/paste them here?

#1369243
Screen Shot 2019-10-24 at 13.15.28.png

Luo,

Here's the code that I have in the "Search and Pagination" section of a View:

<div class="tbdp filters">
[wpv-post-body view_template="view-by-filter"]
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="form-group wpv-wpcf-price-range">
	<label for="wpv-wpcf-price-range">[wpml-string context="wpv-views"]Filter By Price Range:[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-price-range" type="select" source="custom" url_param="wpv-wpcf-price-range" values="3,2,1" display_values="[cas_get_artwork_value_ranges]" default_label="All"]
</div>
[/wpv-filter-controls]
[wpv-filter-end]
</div>

I've also attached a screenshot.

Saul

#1369559

Thanks for the details, you can display the search form and search result in different place.

You try these:
1) Wrap all those codes into shortcode [wpv-filter-start hide="false"] ... [wpv-filter-end], for example:

[wpv-filter-start hide="false"]
<div class="tbdp filters">
[wpv-post-body view_template="view-by-filter"]
[wpv-filter-controls]
<div class="form-group wpv-wpcf-price-range">
    <label for="wpv-wpcf-price-range">[wpml-string context="wpv-views"]Filter By Price Range:[/wpml-string]</label>
    [wpv-control-postmeta field="wpcf-price-range" type="select" source="custom" url_param="wpv-wpcf-price-range" values="3,2,1" display_values="[cas_get_artwork_value_ranges]" default_label="All"]
</div>
[/wpv-filter-controls]
</div>
[wpv-filter-end]

2) Follow our document to display the search form in where you want:
https://toolset.com/documentation/user-guides/front-page-filters/#4-1-adding-the-custom-search-form-to-a-page-or-widget

See screenshot:
https://toolset.com/wp-content/uploads/2012/06/choose-what-part-of-the-custom-search-to-display.jpg

#1370329
Screen Shot 2019-10-25 at 13.24.42.png

Luo,

Thanks for your response. I have to admit I'm confused by the instructions in step 1. They seem to recommend creating a shortcode. I tried removing the snippet from the "Search and Pagination" section of my view, putting it in a PHP file and wrapping it in a shortcode, and WordPress wouldn't let me save the page with just the shortcode in the "Search and Pagination" section. The error messages are visible in the attached screenshot.

The instructions in step 2 to insert the search form also didn't quite feel applicable—or the way I needed to apply them in my case wasn't straight-forward. Do I need to nest a view (with the custom search settings) into the two existing views where I currently duplicate the "Search and Pagination" filter code? If so, do I insert the nested view into the Loop Editor section of the "parent" views?

Saul

#1371203

Q1) As the screenshot you mentioned above, it is required to put those shortcodes insider "Search and Pagination" section, for example:

[wpv-filter-start hide="false"]
...
[wpv-filter-end]

There is a custom shortcode [cas_filter_snippet] in your screenshot, how do you setup the custom shortcode [cas_filter_snippet], since you did not provide detail steps for duplicate the problem, I am not sure if your custom shortcode [cas_filter_snippet] will work insider Views custom search form, you will need to test it in your own website.

Q2) I don't think nested view will work in your case, for example, there is a post view with custom search form: my-view-1.
This custom search form can work for that specific post view "my-view-1" only.

If you are using nested view, there will be multiple search forms, it might conduct other unexpected results.

#1371679
cas_filter_snippet function definition.png
public face - missing filter control.png
view - search and pagination.png

Luo,

Thanks for your response. I'm still confused. I tried to reorganize the PHP in my shortcode function, but it didn't quite work. The screenshot of the result ("public face - missing filter control.png") doesn't include the filter, which is supposed to be located to the right of the "View By" area. (I can't really show you what isn't visible! If you look at the live site, at hidden link, you'll see the filter.)

I've attached a screenshot of the "Search and Pagination" section of the relevant view ("view - search and pagination.png") and the function definition of the shortcode ("cas_filter_snippet function definition.png").

Saul

#1371825

Thanks for the details, In your case, you are using all Toolset shortcodes, I don't think it needs other custom shortcode, you can try as I mentioned above:
https://toolset.com/forums/topic/question-about-custom-search-filter-code/#post-1369559

If you need more assistance for it, please provide a test site with the same problem, I can try to setup a demo for you.

Private message box enabled.

#1372781

Thanks for the details, I can log into your website, will update here if there is anything found.

#1372785

Here are what I found, the URL you mentioned above is archive page of post type "Artist", so normal post view won't work as expected in archive page, especially the pagination, those pagination links are for archive page query, they are not for normal post views: "View of Artist Index" and "View of Artist Index".

I have setup a demo in your website, here are detail steps:
1) Edit the layout "Layout for Artist Archives":
hidden link
At the bottom, add a "WordPress Archive cell", display below Views WordPress archive:
hidden link

2) Test it in front-end:
hidden link

just below text: Below is Views WordPress archive cell
Both the custom search and pagination work fine.

For the switching "text" and "grid" problem: there is a problem in your custom shortcode [cas_get_artist_archive_view], it does not work in WordPress Archive cell, see it in above WordPress archive:
hidden link
you can try to display that shortcode directly: [cas_get_artist_archive_view], it does not output anything.

Section "WordPress Archive - loop item", I have tried below codes:

[wpv-conditional if="( '[cas_get_artist_archive_view]' eq 'text' )"]
[wpv-post-body view_template="loop-item-in-view-of-artist-index-text"]
[/wpv-conditional]

[wpv-conditional if="( '[cas_get_artist_archive_view]' ne 'text' )"]
[wpv-post-body view_template="loop-item-in-view-of-artist-index-grid"]
[/wpv-conditional]

It should be able to output different content template on shortcode [cas_get_artist_archive_view] value.

Since you did not provide details for the custom shortcode [cas_get_artist_archive_view], you will need debug your custom PHP codes of shortcode [cas_get_artist_archive_view], make sure it works in WordPress archive.

More help:
https://toolset.com/documentation/user-guides/wordpress-archive-cell/

#1373175

Luo,

Thanks for setting up the demo on my site and explaining your process.

The problem I'm facing now isn't with the custom shortcode, [cas_get_artist_archive_view], it's the fact that the content template the Views Archive Cell is referencing, [wpv-post-body view_template="view-by-filter"], isn't displaying any content at all. (I put in a line of text outside the conditionals you referenced, and nothing appears on the public face of the page.)

At this point, I'm going to stop working on this issue to focus on other parts of the site.

Thank you for investing all of this time to try to fix the problem. I may reopen the issue in the future.

Saul