最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Check if searched number is within the post meta value

programmeradmin1浏览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?

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
  • '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: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 Commented Sep 25, 2019 at 16:43
  • @WebElaine so do you suggest a foreach on the result and unset the unwanted values – sfarzoso Commented Sep 25, 2019 at 16:51
  • No, set up an array first that contains all the possible values, and use that in your args. – WebElaine Commented Sep 25, 2019 at 18:26
  • @WebElaine could you show me an example? – sfarzoso Commented Sep 25, 2019 at 20:38
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论