通过数字自定义字段订购wp_query

时间:2014-06-23 10:44:20

标签: php sql wordpress advanced-custom-fields

我正在尝试允许用户使用数字自定义字段值对wp_query的结果列表进行排序。用户可以单击按钮来更改按值排序,但是当选择“价格”时,这就是wp_query的样子:

'meta_key' => 'adult',
'orderby' => 'meta_value_num',
'order' => 'ASC',

该字段是ACF(正在使用ACF 5测试版)的数字字段,step大小为0.01,允许价格为9.99。

但是,如果我运行此查询,则每次都不返回任何结果,即使确实存在应该匹配的结果。

我出错的任何想法?

编辑 - 完整查询

$keywordString = $_SESSION['search']['keyword'];
$keywords = explode(', ', $keywordString);

$taxQuery = array(
    'relation' => 'AND',
    array (
        'taxonomy' => 'main-cat',
        'field' => 'slug',
        'terms' => $_SESSION['search']['cat']
    )
);

if( $_SESSION['search']['keyword'] != '' ) {
    $taxQuery[] = array(
        'taxonomy' => 'sub-cat',
        'field' => 'name',
        'terms' => $keywords
    );
}

$args = array(
    // general
    'post__in' => $postIDs,
    'post_type' => 'event',
    'posts_per_page' => 10,
    'paged' => $paged,
    'cache_results' => false,

    'meta_key' => $_SESSION['search']['sort-key'], // becomes 'adult'
    'orderby' => $_SESSION['search']['sort-by'], // becomes 'meta_value_num'
    'order' => 'ASC',

    // category filter
    'tax_query' => $taxQuery,

    // date filter
    meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'date_%_start-date',
            'value' => $when,
            'compare' => '>=',
            'type' => 'NUMERIC'
        ),
        array (
            'key' => 'date_%_end-date',
            'value' => $when2,
            'compare' => '<=',
            'type' => 'NUMERIC'
        )
    )


);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );

0 个答案:

没有答案