I have added a custom taxonomy called experience to the page post-type, and the taxonomy has terms "vfds" and "plcs"
I am trying to create a search where a user can select one or more experience taxonomy terms from a select list, press the submit button, and see the results of a wp_query.
I need the query result to show pages with
1) the experience taxonomy has no (zero) terms selected
2) one or more terms selected
I have an html select list with an array experience[]
to capture multiple selections:
<select name="experience[]" multiple="multiple">
<option value="vfds">VFDs</option>
<option value="plcs">PLCs</option>
</select>
Then I feed this array into a wp_query:
<?php
$_GET['submit'];
$experience = $_GET['experience'];
$args = array
(
'post_type' => 'page',
'tax_query' => array(
array(
'taxonomy' => 'experience1',
'field' => 'slug',
'terms' => $experience
)
)
);
$query = new WP_Query( $args );
The query works fine in the sense that it will show pages that have one or more terms selected under the experience category.
The problem I am having is this:
IF a page has empty (no) values for the experience category, then it is not shown in the result list.
Is there a way where wp_query will show pages with
- no terms selected
- one or more terms selected
Thank you
EDIT #1
Please let me add that in general I am trying to move away from using Drupal with Views. I am trying to replicate Views capability of being able to search using select fields with multiple values.
I have tried to use wp_query
with a custom select field with multiple values, and failed miserably so far.
Hence I switched to using categories with multiple terms and am close to replicating a Views type search result.
If anyone knows any other way of doing what I would like to do in WP, I would very much appreciate it.
Thanks again!
EDIT #2
I realize maybe my question does not make full sense. Let me try and explain further. The reason I need to search result to show pages with ZERO or more terms instead of ONE or more terms, is that I would like to combine the taxonomy search with a another field search, eg City. If a user fills in only a value for city, and no value(s) for experience terms, the search results will be zero because he did not fill in terms. Instead I need to show all pages with the city that was selected, whether or not any experience terms are selected
$args = array ( 'post_type' => 'page', 'tax_query' => array( array( 'taxonomy' => 'experience1', 'field' => 'slug', 'terms' => $experience ) ), 'meta_query' => array( array( 'key' => 'last_name ', 'value' => $lastname, 'compare' => 'LIKE' ) ) );
$query = new WP_Query( $args );