Php Wordpress中“我的自定义帖子类型”中产品的“按ACF排序”字段

Php Wordpress中“我的自定义帖子类型”中产品的“按ACF排序”字段,php,wordpress,sorting,custom-post-type,advanced-custom-fields,Php,Wordpress,Sorting,Custom Post Type,Advanced Custom Fields,您能告诉我如何在ACF中按字段(见图)对我的产品(这是我的自定义帖子类型)进行排序吗 这是我的自定义字段代码: /******* *******/ /* CUSTOM POST TYPE */ /******* *******/ // Custom Post Type - product function register_post_product() { $labels = array( 'name'

您能告诉我如何在ACF中按字段(见图)对我的产品(这是我的自定义帖子类型)进行排序吗

这是我的自定义字段代码:

    /*******            *******/
/*   CUSTOM POST TYPE     */
/*******            *******/

// Custom Post Type - product
function register_post_product() {
  $labels = array(
    'name'               => __( 'Products', '_tk' ),
    'singular_name'      => __( 'Product', '_tk' ),
    'add_new'            => __( 'Add product', '_tk' ),
    'add_new_item'       => __( 'Add New product', '_tk' ),
    'edit_item'          => __( 'Edit product', '_tk' ),
    'new_item'           => __( 'New product', '_tk' ),
    'all_items'          => __( 'All products', '_tk' ),
    'view_item'          => __( 'View product', '_tk' ),
    'search_items'       => __( 'Search product', '_tk' ),
    'not_found'          => __( 'No product found', '_tk' ),
    'not_found_in_trash' => __( 'No product found in the Trash', '_tk' ), 
    'parent_item_colon'  => '',
    'menu_name'          => __( 'Products', '_tk' ),
  );
  $args = array(
    'labels'            => $labels,
    'hierarchical'      => true,
    'supports'          => array( 'title', 'editor', 'page-attributes', 'thumbnail' ),
    'public'            => true,
    'show_ui'           => true,
    'show_in_menu'      => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive'       => true,
    'rewrite'           => array('slug' => 'produkty','with_front' => false),
    'menu_position'     => 6,
    'menu_icon'         => 'dashicons-hammer'
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'register_post_product' );
我在谷歌上做了一些搜索,我试着这样分类:

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
      $query->set( 'orderby', 'meta_value_num' );
      $query->set( 'order', 'ASC' );
      $query->set( 'meta_query','capacity');

      return $query;
}

但是没有结果。

在wordpress中,尝试下面的功能来发布pre\u get\u帖子

<?php
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {

  $query->set( 'post_type', 'product' );
  $query->set( 'meta_key', 'capacity' );
  $query->set( 'orderby', 'meta_value' ); // meta_value_num or meta_value
  $query->set( 'order', 'ASC' );


  return $query;
}
?>

最有可能的问题是
$query->set('meta_query','capacity')

meta_查询应该是一个数组,如

array( array('key' => 'capacity', 
             'value' => array(0, 1000), 
             'compare' => 'BETWEEN',
             'type' => 'numeric', 
     ));

我不知道我打的是否正确?”添加_过滤器('pre_get_posts'、'my_get_posts');函数my_get_posts($query){$query->set('orderby','meta_value_num');$query->set('order','ASC');$query->set('meta_query',array('key'=>'capacity','value'=>array(0,10000),'compare'=>'BETWEEN',);返回$query;},因为不起作用:(你的代码似乎是正确的,这是我的错。我更新了答案中的代码,meta_查询是嵌套数组。但是@shital marakana的解决方案更好,应该也能工作。如果在WP-config中禁用WP_调试,你可能会得到一个空白页。phpIt仍然是@shital marakana的解决方案有问题。我评论了两个l。)ines:
$query->set('post_type','product');$query->set('meta_key','capacity'))
现在产品出现了,但仍然没有排序。你知道有什么问题吗?据我所知,只有帖子列表是空的,页面的其余部分是正常的,因此没有php错误,对吗?那么也许你可以试着查看数据库。如果你已经确定产品在_posteta表中有“capacity”meta,你可以检查最终的sql WP g例如,运行< <代码>全局$WPQQu查询;VARXDUMP($WPQQuest->请求);< /代码>(如果是主查询和<代码>全局$WPL查询< /代码>),并尝试执行它,并查看它为什么不返回结果。可能有一些不明显的条件,如POST状态等。不幸的是,我得到了空白的BAGE结果。