I wrote a function which search a list of properties by specify the number of guests. Each property have a specific number of guests, eg: 10, so if the user search as guests 5
the query should return that post, but my code wouldn't:
$guests_tax_array = array(
'taxonomy' => 'property-guests',
'field' => 'term_id',
'terms' => array($guests), //Searched value from $_POST['guests']
'compare' => 'IN'
);
Essentially, the query should check if 5
(searched value) is contained in 10
using the compare
operator.
But the result is empty, is something wrong there?
I wrote a function which search a list of properties by specify the number of guests. Each property have a specific number of guests, eg: 10, so if the user search as guests 5
the query should return that post, but my code wouldn't:
$guests_tax_array = array(
'taxonomy' => 'property-guests',
'field' => 'term_id',
'terms' => array($guests), //Searched value from $_POST['guests']
'compare' => 'IN'
);
Essentially, the query should check if 5
(searched value) is contained in 10
using the compare
operator.
But the result is empty, is something wrong there?
Share Improve this question asked Sep 25, 2019 at 14:22 sfarzososfarzoso 498 bronze badges 5 |1 Answer
Reset to default 1Assuming your "property-guests" taxonomies have names like "1", "2", "3", etc.:
<?php
// Get the number of guests the visitor searched for
$requiredGuests = $_POST['guests'];
// Get all terms in the "property-guests" taxonomy
// (By default, this will only include terms that are assigned to something)
$allGuestNumbers = get_terms(array('taxonomy' => 'property-guests'));
// Create a variable to use in the args later
$queryNumbers = '';
// Determine which properties have enough room for the number of guests requested
foreach($allGuestNumbers as $guestNumber) {
// Check if the number is greater than or equal to the number requested
if($guestNumber->name >= $requiredGuests) {
// If so, add to the variable for args, with a comma to separate the numbers
$queryNumbers .= $guestNumber->name . ',';
}
}
// Now, use the variable in the args
$guests_tax_array = array(
'taxonomy' => 'property-guests',
'field' => 'term_id',
'terms' => $queryNumbers,
'compare' => 'IN'
);
?>
If your "property-guests" taxonomies don't have digits as their names, you could use their slugs, or a substring of the name or slug - it just depends on how you set up the individual terms.
'compare' => 'IN'
actually means, "see if this exact value is contained in the field." So, 5 will never be "in" 10. I don't think a WP tax query actually supports checking whether a numeric value is "less than or equal to" a number, which is what you're looking for. – WebElaine Commented Sep 25, 2019 at 16:41property-guests
. So for example, maybe you have properties with room for 4, 7, and 10 guests. If a visitor searches for 5, you first get all theproperty-guests
numbers and isolate the ones that are greater than or equal to 5 (the search). Add those all to an array (at this point, an array including 7 and 10). Then, pass that array interms
in your query and you will find all the properties that have room for that many guests. – WebElaine Commented Sep 25, 2019 at 16:43