[Resolved] I need to exclude some pages from the custom search results view

This thread is resolved. Here is a description of the problem and solution.

Problem: The user is using a custom field on pages and he would like to create a search view that will search only pages that have a value for this custom field.

Solution: You can get this with two solutions:

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.

This topic contains 8 replies, has 2 voices.

Last updated by panayiotisS 1 month, 2 weeks ago.

Assigned support staff: Jamal.

Author
Posts
#1578389

Tell us what you are trying to do? I have built 2 custom post types for my project pages and i added them only in the projects pages. Then i have built a custom search form to filter the pages according to the custom post types i added to them earlier. This works fine. The problem is that in the toolset view with the search results, by default appears all the pages of my website. How can i exclude the pages which are not projects or don't have the custom post types?

Thanks

#1578681

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

To better answer your question, I'll need more details on your website. What custom post types are involved? Are there any relationships? Where is the view used?

You may also allow me temporary access to your site to check this further. But I'll need more details, which page? Steps to follow for testing? Expected results?
Your next reply will be private to let you share credentials safely.
** Take a database backup before sharing credentials **

#1579611

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello, can you answer the questions on my last reply and allow me access to your website to check this further? Your next reply will be private to let you share credentials safely. Please make a database backup before sharing credentials.

#1579987

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I believe that your view is querying pages instead of projects, check this screenshot hidden link
Once you deactivate pages and activate projects, it will show projects that met the search criteria.

Projects and Pages are different post types. If you would like to search both and still exclude some pages, let me know and we'll find a way to do it.

If you want to filter only projects, implement my suggestion on the second line.

#1580001

I want to filter only specific pages and not project post types.

I made project post types just in case i couldn't get the pages filtering to work as i wanted.

If you go to the website you can see in the main menu a page called projects. In that page there are multuple projects which have each their own page.

Those are the only pages i need in the results of toolset view.

#1580155

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you for your feedback. Now I understand better your use case. You are using custom fields on pages for project data, and you want to return only the pages that have these fields filled, right?

This will need custom code to implement it because you have already set a filter on the custom fields equal to the value of the url parameter, we cannot add another filter on the same field with the user interface that can check for the existence of the value of the field.
I run a local test and I was able to do it with the following code on my theme functions.php file:

add_filter( 'wpv_filter_query', 'exclude_non_projects_pages', 30, 3 );
function exclude_non_projects_pages( $query_args, $view_settings, $view_id ) {
	// Execute this code only for my view id(1186)
	if ( $view_id == 1186 ) {
		if ( ! isset($query_args["meta_query"] ) ) {
			$query_args["meta_query"] = array(
				array(
					'key' => 'wpcf-project-area',
					'compare' => 'EXISTS'
				),
			);
		}
	}
	
	// return the query arguments
	return $query_args;
}

Note that this code only checks if the value of "Project Area" is provided, for more use cases, please check the documentation for the meta_query parameter here https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

You will need to change (1186) with your view id (4641).

Because the FTP credentials were not provided, I did not test it my self on your website. Please check it and let me know if this works for you.

#1580183

So i will have to paste this code with the change you mentioned (1186) with your view id (4641) in my functions.php file?

#1580881

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I got a hint from our 2nd Tier to get what you want without this code. The trick is to use one of the fields in "Ordering", I tested it locally and it gave the same results, only the pages that have a value for the field were returned.
Check this screenshot for my view hidden link

I remain at your disposal.

#1580995

My issue is resolved now. Thank you!