简体   繁体   English

按meta键对Wordpress中的类别帖子进行排序

[英]Sort category posts in Wordpress by meta key

I will try to make this as short and clear as possible. 我将尝试使它尽可能简短。

What is my goal? 我的目标是什么?

My goal is to sort posts in my category page by meta key. 我的目标是通过meta键对我的类别页面中的帖子进行排序。 I have a category named "Rating" and each post in that category has a meta key named "Rating" - in it is a numerical value. 我有一个名为“ Rating”的类别,该类别中的每个帖子都有一个名为“ Rating”的元键-它是一个数值。

So, for example, I have "Restaurant A" and "Restaurant B" in category "Rating". 因此,例如,我在“评级”类别中有“餐馆A”和“餐馆B”。 One has meta key "Rating" value of 56, the other one 40. I would like to show them from the highest rating to the lowest rating (not alphabetically, chronologically or any other way.) 一个具有元密钥“ Rating”值56,另一个40。我想显示它们的等级从最高到最低(不是按字母顺序,按时间顺序或其他方式)。

What have I tried? 我尝试了什么?

I have tried adding this directly into my category.php file: 我尝试将其直接添加到我的category.php文件中:

<?php global $query_string;
$posts = query_posts('posts_per_page=5&cat=rating&meta_key=rating&order=ASC'); 
?>

before this part, which was already in category.php file: 在这部分之前,已经在category.php文件中:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php fotomag_post_layout(); ?>
<?php endwhile; ?>
<?php endif; ?>

and ended the code with this part: 并以这一部分结束代码:

<?php wp_reset_query(); ?>

How did I fail? 我怎么失败了?

It partially works. 它部分起作用。 It does show, for example, 5 posts and if I delete meta key from some of the posts in the "Rating" category it doesn't show them here (which is correct - because the query is to show posts in Rating category which have a Rating metafield). 例如,它确实显示了5个帖子,并且如果我从“评分”类别中的某些帖子中删除了元键,它就不会在此处显示(这是正确的-因为查询是要显示“评分”类别中的帖子,其中包含评分元字段)。 But it does not order it by ascending or descending means. 但是它不是通过递增或递减的方式对其进行排序。

What am I doing wrong? 我究竟做错了什么? Should I put any kind of filter or action hook or something in my functions file? 我是否应该在函数文件中放置任何类型的过滤器或动作挂钩? Is something messing up with my theme? 我的主题搞乱了吗? (I have no other plugins installed by the way) (我没有安装其他插件)

Additional files: 附加文件:

My functions.php file (with the add_action hook mentioned in the answer below) - https://pastebin.com/WYyRknD6 我functions.php文件(在下面的答复中提到的ADD_ACTION钩) - https://pastebin.com/WYyRknD6

My category.php file - https://pastebin.com/nWFgL1bV 我的category.php文件-https://pastebin.com/nWFgL1bV

My post layout file (this is the file that gets called in the category.php file - it maybe is or isn't relevant) - https://pastebin.com/3M6mrQVY 我的帖子布局文件(这是在category.php文件中被调用的文件-可能相关或不相关) -https://pastebin.com/3M6mrQVY

You can check the live version of the page here - https://preprod.pojej.me/category/rating/ - the number shown on the post is the number value in the "Rating" meta key field (minus the % symbol). 您可以在此处查看页面的实时版本-https: //preprod.pojej.me/category/rating/-帖子中显示的数字是“评分”元键字段中的数字值(减去%符号) 。

Instead of that, you'll want to use the pre_get_posts filter to modify the main query and have it return posts ordered by your meta field. 取而代之的是,您将要使用pre_get_posts过滤器修改主查询,并使其返回按您的meta字段排序的帖子。 This way, if you want to extend this functionality to other categories (or taxonomies) later you'll only need to change the code in one place. 这样,如果您以后想将此功能扩展到其他类别(或分类法),则只需要在一个地方更改代码即可。

For example: 例如:

function wp87615486_custom_sort_rating_category( $query ) {

    /**
     * Modify the 'rating' category main query
     * to have it sort posts by meta rating value.
     */
    if (
        is_category( 'rating' ) // we're currently viewing the 'rating' category
        && !is_admin() // and we're not in the admin
        && $query->is_main_query() // and this is WP's main query
    ) {

        $posts_per_page = get_option( 'posts_per_page' );

        $query->set( 'posts_per_page', $posts_per_page );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_key', '_lets_review_final_score' );
        $query->set( 'order', 'DESC' ); // ASC will return entries with the lowest rating, DESC the ones with the higher rating

    }

}
add_action( 'pre_get_posts', 'wp87615486_custom_sort_rating_category' );

Then you can use the regular loop in category.php. 然后,您可以在category.php中使用常规循环

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

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