检查搜索的数字是否在帖子元值内

时间:2019-09-25 作者:sfarzoso

我编写了一个函数,它通过指定来宾数量来搜索属性列表。每个属性都有特定数量的来宾,例如:10,因此如果用户以来宾身份搜索5 查询应该返回该帖子,但我的代码不会:

$guests_tax_array = array(
    \'taxonomy\' => \'property-guests\',
    \'field\' => \'term_id\',
    \'terms\' => array($guests), //Searched value from $_POST[\'guests\']
    \'compare\' => \'IN\'
);
本质上,查询应该检查5 (搜索值)包含在10 使用compare 操作人员

但结果是空的,有什么不对吗?

1 个回复
SO网友:WebElaine

假设您的“property guests”分类有“1”、“2”、“3”等名称:

<?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\'
);
?>
如果您的“属性来宾”分类法没有数字作为其名称,您可以使用其slug,或名称或slug的子字符串-这取决于您如何设置各个术语。

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post