0

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?

  • '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 Sep 25 '19 at 16:41
  • You may need to first run a query to find all possible values of property-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 the property-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 in terms in your query and you will find all the properties that have room for that many guests. – WebElaine Sep 25 '19 at 16:43
  • @WebElaine so do you suggest a foreach on the result and unset the unwanted values – sfarzoso Sep 25 '19 at 16:51
  • No, set up an array first that contains all the possible values, and use that in your args. – WebElaine Sep 25 '19 at 18:26
  • @WebElaine could you show me an example? – sfarzoso Sep 25 '19 at 20:38
1

Assuming 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.