WordPress:如何通过ACF自定义字段对内容进行排序? [英] WordPress: How to sort content by ACF custom field?

查看:285
本文介绍了WordPress:如何通过ACF自定义字段对内容进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用高级自定义字段插件,我创建了一个选择下拉列表,其中包含6种成员资格类型。我使用此自定义字段的所有列表都分配了6个值。

With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.

我想按以下方式显示所有列表:

I'd like to display all 'listings' by:

Ultimate Plus

Ultimate

Professional

Commercial

Business

Free

Ultimate Plus
Ultimate
Professional
Commercial
Business
Free

按照此特定顺序,那些支付最高级别会员资格的人就会在页面顶部显示列表。

In this particular order, so those paying for the highest level membership have their 'listing' appear at the top of the page.

我希望它与我刚刚发现但不确定的类似:

I expected it to be similar to this which I just found but unsure exactly:

// args
$args = array(
'post_type'  => 'directory_listings',
'meta_key'   => 'free',
'orderby'    => 'meta_value_num',
'order'      => 'ASC',
'meta_query' => array(
    array(
        'key'     => '#',
        'value'   => array( #, # ),
        'compare' => 'IN',
    ),
),
);

// query
$wp_query = new WP_Query( $args );

?>

<?php if (have_posts()) : ?>

    <?php
    while( $wp_query->have_posts() ) {
        the_post();
        ldl_get_template_part('listing', 'compact');
        ldl_get_featured_posts();
    }
    ?>

<?php else : ?>

<?php endif; ?>


推荐答案

您快到了

如果将高级自定义字段中的选择更改为

If you change the choices in the Advanced Custom Fields to

1 : Free
2 : Business
3 : Commercial
4 : Professional
5 : Ultimate
6 : Ultimate Plus

然后将默认值设置为1

And then the default to 1

通过执行此操作,您将值设置为数字,默认值为Free值。在管理屏幕中,您将看到文本值。

By doing this you are setting the values to the number and the default is the Free value. In the admin screen you will be presented with the text value.

然后要执行查询,请尝试为此查询

Then to do your query try this for the query

$wp_query = get_posts(array(
  'numberposts' => -1,
  'post_type' => 'directory_listings',
  'meta_key' => 'membership_type',
  'orderby' => 'meta_value',
));

它将获取所有设置了值的帖子,并按成员资格类型降序排列是您想要的。

It will get all of the posts that have a value set and order it by the membership type descending which is what you want.

我已经在本地设置中尝试过此操作,以确认这一点。

I have tried this on my local setup to confirm this.

这篇关于WordPress:如何通过ACF自定义字段对内容进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆