Wordpress meta_query不使用' orderby' - 高级自定义字段

时间:2014-08-08 22:33:59

标签: php mysql wordpress

我正在尝试查询自定义帖子类型“部分”,其中'section.section_parent_page = {无论当前$ post的ID是什么}。

这部分查询工作正常,完成如下:

$args = array(
    'post_type' => 'section',
    'meta_query' => array(
        array(
            'key' => 'section_parent_page', // name of custom relate field
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
);

但是一旦我尝试'orderby'自定义字段'section_position',我要么打破查询,要么看不到正确的顺序。这是我现在拥有的:

$args = array(
    'post_type' => 'section',
    'meta_key' => 'section_position', // custom field I want to order by
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'section_parent_page', // name of custom relate field
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
);

当我查看$ the_query->请求时,看起来它继续按wp_posts.menu_order排序

如何使用多个meta_query数组来实现此目的?

编辑: 无论我尝试什么,我的查询字符串的结尾看起来像这样:

ORDER BY wp_posts.menu_order ASC LIMIT 0, 10

2 个答案:

答案 0 :(得分:1)

如果section_position具有整数(数字)值,请使用以下代码

$args = array('post_type' => 'section',
              'orderby' => 'meta_value_num', 
              'meta_key' => 'section_position',
              'order'=>'ASC',
              'meta_query' => array(
                               array(
                                 'key' => 'section_parent_page', // name of custom relate field
                                 'value' => '"' . get_the_ID() . '"',
                                 'compare' => 'LIKE'
                               )
                              ) 
               );

注意我使用了“meta_value_num”而不是“meta_value”

答案 1 :(得分:0)

我最近尝试在ACF过滤器中做同样的事情。我联系了ACF支持并收到了这个答案。

  

不幸的是,我的查询过于复杂,无法通过WP_Query传递,您必须执行手动SQL查询才能实现此目的。   问题是你正在做两个meta_querys,meta_key =开始日期和括号内的那些是冲突的。   您需要完整SQL语法的灵活性来执行类似的查询。