排序我的自定义文章中的产品中的ACF字段在Wordpress中
请问如何根据ACF中的字段(参见图片)对我的产品(这是我的自定义帖子类型)进行排序?排序我的自定义文章中的产品中的ACF字段在Wordpress中
这里是我的自定义字段代码:
/******* *******/
/* 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
<?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;
}
?>
试试这个下面功能pre_get_posts
最有可能的问题是$query->set('meta_query','capacity');
meta_query应该是一个数组,像
array(array('key' => 'capacity',
'value' => array(0, 1000),
'compare' => 'BETWEEN',
'type' => 'numeric',
));
我不知道我是否正确输入了它? 'add_filter('pre_get_posts','my_get_posts'); function my_get_posts($ query){ \t $ query-> set('orderby','meta_value_num'); \t $ query-> set('order','ASC'); \t $查询 - >置( 'meta_query',阵列( '键'=> '能力', '值'=>数组(0,10000), '比较'=> '之间', )) ; \t \t return $ query; }“ 因为没有工作:( –
您的代码似乎是正确的,这是我的不好,我更新了答案的代码,meta_queries嵌套数组。但@清淳,marakana的解决方案,甚至更好,应该工作以及。如果WP_DEBUG在wp-config.php中被禁用,你可能会得到一个空白页面 – c0der
@Shital-Marakana的解决方案仍然有问题,我评论了两行: '$ query-> set(' ('meta_key','capacity');' 和现在的产品出现但仍然未排序。任何想法可能是错误的? –
不幸的是我重新拥有空白包sult –