[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.
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?
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 **
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.
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.
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;
}
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