简体   繁体   English

按“ACF”自定义字段排序帖子

[英]Sort Posts by 'ACF' custom field

I have wordpress website with enfold theme.我有包含主题的 wordpress 网站。 i want to sort the post within a masonry based on an id that i added to the posts using Custom fields.我想根据我使用自定义字段添加到帖子中的 id 对砌体中的帖子进行排序。 for example the post titles are a, b, c.例如帖子标题是 a、b、c。 each one of them has an id.他们每个人都有一个id。 and they belong to category "cat" and the cat category is selected in masonry.它们属于“猫”类别,猫类别是在砌体中选择的。 now i want to sort the items in masonry by my custom id.现在我想按我的自定义 ID 对砌体中的项目进行排序。 this link 这个链接

defines a function for customizing sort options.定义了一个 function 用于自定义排序选项。 how can i add my meta key to this code?如何将我的元密钥添加到此代码中? i also tried to use this but the key is not displaying in sort options.我也尝试使用,但排序选项中没有显示密钥。

I did something similar recently, possibly this code will help you out.我最近做了类似的事情,可能这段代码会帮助你。 Add it to your functions.php将其添加到您的函数中。php

add_filter('posts_orderby','my_sort_custom',10,2);
function my_sort_custom( $orderby, $query ){
    global $wpdb;

    if(!is_admin() && is_category()) 
        $orderby =  $wpdb->prefix."postsmeta.MY_META_KEY ASC, {$wpdb->prefix}posts.post_date DESC";

    return  $orderby;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM