在 Wordpress 模板的 function.php 中,以下代码运行良好
// Function accepting current query
function my_change_order( $query ) {
// Check if the query is for an archive
if($query->is_archive())
// Query was for archive, then set order
$query->set( 'order' , 'asc' );
// Return the query (else there's no more query, oops!)
return $query;
}
// Runs before the posts are fetched
add_filter( 'pre_get_posts' , 'my_change_order' );
但我需要通过自定义元键(如 _my_meta_vip
)来订购文章。基于this answer我尝试了以下几行,但成功了一半,因为只加载了具有定义的自定义元键的文章,其他的都丢失了。我该如何解决?
function my_change_order( $query ) {
if($query->is_archive())
$query->set( 'orderby' , 'meta_value' );
$query->set( 'meta_key' , '_my_meta_vip' );
return $query;
}
add_filter( 'pre_get_posts' , 'my_change_order' );
如何通过自定义元键正确地订购我的文章?
最佳答案
function my_change_order( $query ) {
// Check if the query is for an archive
if($query->is_archive())
// Query was for archive, then set order
$query->set( 'order' , 'asc' );
$query->set( 'meta_query', array(
array(
'key' => '_my_meta_vip'
)
));
// Return the query (else there's no more query, oops!)
return $query;
}
请看以下主题:https://wordpress.stackexchange.com/questions/20237/using-meta-query-how-can-i-filter-by-a-custom-field-and-order-by-another-one可能会给你一个清晰的思路
关于php - WP 存档按自定义元键排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30361635/