php - WP 存档按自定义元键排序

标签 php wordpress

在 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/

相关文章:

php - CSS 不适用于我的 PHP 代码(回显/无回显)

cron - PHP - 最后一次通话

javascript - AJAX jquery post 数据对于浏览器/客户端操作是否安全

php - 防止 str_replace 注释字符串中的 php 代码

wordpress - 通过 `woocommerce_view_order` 显示 ACF 字段

wordpress - 如何在主题激活时填充侧边栏上的小部件

jquery - WordPress 和 Jquery : How to call a wordpress function with jquery on click event of a div?

php - 如何显示用户配置文件中的 acf 字段?

php - 网关超时 :The gateway did not receive a timely response from the upstream server

css - 无论我尝试什么,标题都不会改变颜色