Order by在自定义帖子类型中的WordPress自定义查询中不起作用

v6ylcynt  于 2023-03-22  发布在  WordPress
关注(0)|答案(1)|浏览(169)

我使用ACF Pro Repeater字段,我在数据库中的自定义字段名称为seminars_details_0_start_date,seminars_details_1_start_date等
下面是我使用的代码

function my_posts_where( $where ) {
            $where = str_replace("meta_key = 'seminars_details_$", "meta_key LIKE 'seminars_details_%", $where);
            return $where;
          }
          add_filter('posts_where', 'my_posts_where');
          $args = array(
           // 'numberposts' => ,
            'posts_per_page'=> '-1',
            'post_type'   => 'training',
            'meta_key'      => 'seminars_details_$_start_date',
            'orderby'     => 'meta_value',
            'order'       => 'DESC',
            'meta_query'  => array(
              'relation'    => 'OR',
             array(
                'key'   => 'seminars_details_$_start_date',
                'value'   => array('20230101'  , '20231231'),
                'compare' => 'BETWEEN',
                'type'    => 'NUMERIC',
              ),
            )
          );

          $the_query = new WP_Query( $args );
3htmauhk

3htmauhk1#

首先,你的代码中有一些缺陷.尝试用下面的代码替换$args-

$args = array(
            

        // 'numberposts' => ,
                'posts_per_page'=> -1,   // number cannot be in quotes 
                'post_type'   => 'training',
                'meta_query'  => array(
                   'key'   => 'seminars_details_$_start_date',
                    'value'   => array('20230101'  , '20231231'),
                    'compare' => 'BETWEEN',
                    'type'    => 'NUMERIC',
                   )
              );
    $args['meta_key'] = 'seminars_details_$_start_date';
    $args['order_by'] = 'meta_value_num';
    $args['order'] = 'DESC';

相关问题