Skip Navigation

[Closed] Set a default State filter on archive page load for locations custom post type

This support ticket is created 2 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.

Our next available supporter will start replying to tickets in about 2.01 hours from now. 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/Karachi (GMT+05:00)

This topic contains 12 replies, has 2 voices.

Last updated by Waqar 2 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2287179

I have an archive page to display all locations in my directory.

Unfortunately to show all the locations in my directory renders the archive with search form and map completely unusable because of the dramatically slow loading times.

To try and find a solution where there isn't one with Toolset I've come up with another idea.

This is to show a default State, for example, "New York", when the archive page loads initially.

By doing so, it will intuitively suggest to users to select their own state from the drop down menu, while at the same time, when limiting the locations on the map to only one state it will load far fewer locations eliminating the absurd loading time required for hundreds of directory locations on the map.

How can I go about implementing this?

I suppose what I need to do is set a "default" filter parameter in my dropdown selection for filtering locations by State?

Is there a way to implement this which won't change the URL of the archive page to one with the long string query behind it? (to try and keep things clean for SEO purposes)

I haven't found any documentation on how to implement something like this.

Preferably I do not want to create yet a second view for an archive of all locations in "New York" and use conditional blocks to show etc. (as I'm doing this on an archive page, without Toolset Views, I don't think I can anyway)

Is there a simple snippet of code I may be able to insert into the code editor to achieve this?

It's important that the search/filter form remains functional for the users to be able to navigate next to their own state.

Thank you for the help!
G

#2287409

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

If your goal is to show limited results on the archive page, a more intuitive way could be to use pagination to load only a fixed amount of results per page (e.g. 10, 15, or 20, etc).

This way, you won't have to include any customization and visitors will still be able to use the state search filter, as needed.

If for some reason, you'd still prefer to filter by default state (when the page initially loads), you'll need to use the WordPress hook "pre_get_posts", to customize the archive's query:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2288359

Per my other requests for support, all of these efforts are to try and mitigate the slow loading of my listings directory of 600 locations (my next website has over 2000 locations which I need to display also on a map directory).

I don't want to show users a page with only 5 locations on the map when there are 600 in the directory, if I do most won't figure out that you just need to use the search function to see listings near them.

I know many other users have had this same issue as me from the support threads but it doesn't seem like there has ever been a good solution found.

This is why I want to display one state upon initial page load, doing this will provide the best user experience I think.

The map will show many locations in the one state, while being a small enough number that the page load time will be bearable, and intuitively users will select their own state.

Using this code I was able to get the archive page to load with only New York, but the problem is, then the other states are ALL missing from the drop down selector and New York is the only option.

add_action('pre_get_posts', function($query){
if ( ! is_admin() && $query->is_main_query() && is_tax( $taxonomy = 'taxonomy1', $term = 'term1' ) ) {
$query->set( 'state', "new-york" );
}
},99);

I think this must be because this is altering the archive query to only show locations from New York, leaving no other locations to be searched through.

It seems like therefore the Toolset filter drop down for "State" itself will need to be given some default parameters for initial page load?

Please help!

Thanks
G

#2289759

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

classic-archive-search-settings.png
block-archive-search-settings.png

Thanks for writing back.

During testing on my website, the dropdown search field options are only limited, when the archive is set to show only those options in the search form, which can produce results.

To always show all options (states) in the search form, make sure that the 'Show only filter options that would produce results' is disabled in the "WordPress Archive Search" block's settings.
( example screenshot attached: block-archive-search-settings.png )

If you're using the classic/legacy archive editor, you'll see this option under the "Custom Search Settings" section.
( example screenshot attached: classic-archive-search-settings.png )

#2289817

Ok I've unchecked that box.

Then using the same code as before,

add_action('pre_get_posts', function($query){
if ( ! is_admin() && $query->is_main_query() && is_tax( $taxonomy = 'taxonomy1', $term = 'term1' ) ) {
$query->set( 'state', "new-york" );
}
},99);

It now shows all states in the drop down, but when I select another one and click search, then NO post results show up on the map or on the page.

I think this is still because once the archive has been set to show posts from New York you're not going to see any other posts from other states?

Wouldn't this need to be some kind of default setting for search form?

Or do I need a different query command?

In any case, right now, with the code above, only posts in New York appear , even after using the search to select another state.

Still need help!

Thanks!

G

#2290239

I've been trying now to add in some text into the code editor to pre-select the state.

Just getting errors and needing to delete the block and put in a new one.

I notice when I inspect the on page element for the States drop down that right there you see,

<option selected="selected" value="0"></option>

followed by option values equal to each of the different states.

Is there somewhere I can somehow initialize this "selected" variable to a single state?

This must be possible, I'm just not familiar with the code enough..

Out of hundreds of directories made with Toolset, I'm sure people have done this before!

Please help!

I'm just spinning my wheels over here and I need to get this directory up and running.

It's too slow and unusable right now with Toolset defaults and I've been trying to sort out a solution for a balance between page speed and functionality (ie. functionality showing all 600 locations on the map at once, which Toolset has no work around for making the page unusably slow).

I'm working on this everyday and getting no where, someone please help!

G

#2291843

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I can help you with these remaining points, but, I'll need to see exactly how this archive is set up in the admin area.

Can you please share temporary admin login details, along with the link to the archive page?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2294623

I prefer not to share all of this; this website is my only income source so I need to be very careful about logins and there isn't a way to create Read Only permissions.

Can you tell me what pages or where to screen shot and I can upload those screenshots privately instead?

#2294627

I don't seem to be able to privately share the URL's for the pages either.

It would be nice if all support topics weren't exclusively public forum, like having a private email support or private chat support.

I don't see anywhere where I can set my replies to private either except for the first response back to your last message where you requested from me private details.

#2294631

Can you even give me any kind of a clue as to where and what kind of code to add?

Is it going to be in the code editor?

It's not like there are archive page settings under Toolset settings page that changes how the search or initial page loads... I've thoroughly checked through all of the settings trying to resolve this myself.

This is just a regular archive page, with a search function for the posts, and I want the map to initialize on a certain State (taxonomy).

I suppose this could be done through the Address field, or the SEPARATE taxonomy field that I have for State of each post.

I included this taxonomy for State to allow users to search by state.

So really this isn't so much about filtering through the address field , as it is just choosing a single value (State) from my States taxonomy field.

#2295537

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I can understand your concerns about sharing admin access to your website.

We don't make direct changes to client websites and the only reason for requesting the admin access was to see exactly how these taxonomies and archives are set up and do some testing on a test website (and not on your website), accordingly. This eliminates the guesswork and avoids the back and forth communication and delays.

In an earlier reply, you mentioned this code worked for you to limit the results only to the 'New York' state when the page loads:


add_action('pre_get_posts', function($query){
if ( ! is_admin() && $query->is_main_query() && is_tax( $taxonomy = 'taxonomy1', $term = 'term1' ) ) {
$query->set( 'state', "new-york" );
}
},99);

Can you test a slightly updated version of that code snippet?


add_action('pre_get_posts', function($query){
if ( ! is_admin() && $query->is_main_query() && is_tax( $taxonomy = 'taxonomy1', $term = 'term1' ) ) {
	if (empty( $_GET['wpv_filter_submit'] )) {
		$query->set( 'state', "new-york" );
	}
}
},99);

The empty check for '$_GET['wpv_filter_submit']' will make sure that the default state 'New York' is only selected when the page loads and the visitor hasn't performed any search. When the user will select any values from the search form and performs the search, then the code will not set the state taxonomy query to 'New York'.

#2295845
state drop down shows only new york.png

Hey Waqar!

thanks for this response, seems very promising.

And thanks for your understanding.

I tried this out and it, kind of, almost works!

When I implement the updated code, the page loads showing only New York.

However, when I then go to the search form to change the state in the drop down, there is only ONE state in the list, New York.

If I then SELECT "New York" in the search form, and perform the search, the page reloads and shows only New York again.

But now AFTER performing the search with "New York" selected, when I go back to the search form and click the States drop down, now ALL of the states show up (which wasn't happening at all before, so this is a great improvement haha).

Perhaps there is some code snippet that can maintain the other states in the list for that initial page load?

Thanks Waqar this is a huge improvement!!

G

#2298383

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Glad that we're making progress.

Unfortunately, I'm out of ideas and for any further suggestions, I'll need access to this archive page and its settings.

If you're hesitant about sharing the admin access for the live/production website, perhaps you'd be willing to share its clone/snapshot?
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

This way, I'll be able to troubleshoot this on a different server, without affecting the actual website.

Note: I've set your next reply as private.

The topic ‘[Closed] Set a default State filter on archive page load for locations custom post type’ is closed to new replies.