Skip Navigation

[Resolved] Views not having a default value.. I need to display a default value on loadout

This support ticket is created 6 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1143578

Tell us what you are trying to do?
I'm trying to load the Values under Downtown / Pacific City, so it won't load the whole list from my list of businesses under SECTOR.

Right now visitors still need to click on submit for the Businesses under Downtown / Pacific City to be listed, and it loads all the businesses which is not ideal since we have tons of businesses listed. I need the default loadout to filter it to Downtown / Pacific City once a visitor loads our site.

Is there any documentation that you are following?
https://toolset.com/forums/topic/is-it-possible-to-add-placeholder-textdefault-label-to-wpv-filter-search-box/
https://toolset.com/forums/topic/set-filter-to-a-default-value/

Is there a similar example that we can see?

What is the link to your site?
hidden link

New threads created by Nigel and linked to this one are listed below:

https://toolset.com/forums/topic/split-feature-request-default-value-for-views-filter-controls/

#1143786

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Glen

I split this into another ticket to create a feature request for this, as it seems like a good idea for a Views feature, but I'll try and help you set it up here, which will require some coding.

Because you don't have an empty placeholder in the filter the Downtown / Pacific City option already appears selected, so all that is needed is to use the wpv_filter_query API filter to set that option if no other filter is already chosen.

You can try adding the following code to your site (at Toolset > Settings > Custom Code):

/**
 * Set default custom field filter if not already set
 */
function tssupp_filter_query($view_args, $view_settings, $view_id) {

	$slug = 'sector';

	if (in_array($view_id, array(359))) {
		// Edit View ID

		$exists = false;

		if (isset($view_args['meta_query'])) {

			foreach ($view_args['meta_query'] as $key => $meta_query) {

				if (isset($meta_query['key']) && $meta_query['key'] == 'wpcf-'.$slug) {
					$exists = true;
					break;
				}
			}
		}

		if (!$exists) {

			$view_args['meta_query'][] = array(
				'key' => 'wpcf-'.$slug,
				'value' => 1,
				'type' => 'CHAR',
				'compare' => '=',
			);
		}
	}

	return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Check that the slug is correct, and edit the ID of your View.

Do you want to try that and let me know how you get on?

#1144079
no_code_snippet.png

Hi, I just tried to add in the snippet but the portion on the custom code where I insert the code is not showing, it just loads non stop.. any other work around, we can also go with showing nothing to list on page load, then shows the list after the visitors change the values on the search, and clicks in the SEARCH or FIND button on my case.

I'm using OceanWp with elementor, I tried disabling all the plugins and leave the views plugins, but nothing changed, just non stop loading..

#1145054

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Glen

Well, regarding the original question, you could add the code sample I gave to your theme's functions.php file.

Try that and see if it does what you required.

Regarding the problem with the Code Snippets settings page, there would likely be a PHP error if it fails to load. Could you check your error logs?

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor. Try visiting the same Code Snippets page again then inspect the log. If you don't find the debug.log file it means it didn't generate any warnings or errors.