是否可以按分配的相同顺序查询帖子? [英] Is it possible to query posts in the same order as assigned?

查看:67
本文介绍了是否可以按分配的相同顺序查询帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 type 的自定义字段,这是一个单选按钮数据类型,它有一些选择。该自定义字段被分配给名为 pproduct 的自定义帖子类型。

I have a custom field named type, which is a a "radio button" data type and it has some choices. This custom field, is assigned to a custom post type named pproduct.

例如,以下是此自定义字段的选择:

For example here are the choices of this custom field :


  • RED

  • 蓝色

  • 黄色

  • 白色

  • 黑色

  • RED
  • BLUE
  • YELLOW
  • WHITE
  • BLACK

只能从上面选择一个。

下面的 $ args

 $args = array(
      'post_type' => 'pproduct',
      'posts_per_page' => -1,
      'post_status'=>array('publish'),
      'product' => $category->slug ,
      'meta_query' => array(
          'relation' => 'AND',
          'type_clause' => array(
              'key' => 'type',
          ),
          'order_clause' => array(
              'key' => 'order',
          ),
      ),
      'orderby' => array(
              'type_clause' => 'DESC',
              'order_clause' => 'ASC',
      ),
  );

将查询所有帖子类型为 pproduct 的帖子,并对其进行排序由两个自定义字段组成。 类型订单。它将按字母顺序对其进行排序。

will query all posts of post type pproduct, and it will sort it by two custom fields. Type and order . It will sort it in an alphabetical order.

是否可以进行修改,并按照与分配类型相同的顺序对其进行排序?有谁知道我不使用order by会怎样?我可以看到它带来了帖子,但是如果不是我分配的,则是默认顺序。

Is it possible to modify this and sort it by the same order as the types are assigned? Does anyone know what happens if i don't use order by? I can see it brings the posts but what is the "default order" if it's not assigned by me.

编辑1:类似于

推荐答案

更新:

我误解了您的需求。如果我现在知道了,您想按设置中的命令进行订购,就像您写

I missunderstood your demand. If i get it right now, you want to make order as you assigned it in setting, simply like you write


  • RED

  • 蓝色

  • 黄色

  • 白色

  • 黑色

  • RED
  • BLUE
  • YELLOW
  • WHITE
  • BLACK

使用WP查询args无法实现,您将必须编写自己的数据库查询来实现此目的,因为查询必须知道已设置的顺序规则由您(它确实知道字母,数字,日期顺序等,可以简单地从字段中得出)。

That couldn't be achieved with WP query args, you will have to write your own database query to achieve this because query must know the order rules which is set by you (it does know alpabetical, numeric, date order etc. which can be simply derivated from field).

但是,如果您可以将值ACF更改为数字(例如您已在评论链接中张贴),那么您将获胜,那么您将必须创建转换数组(将数字转换为颜色名称),如果您需要将值用作颜色名称/段。
因此,在ACF设置中,选择:

However if you could change values ACF to numeric (like u've posted in comment link) you win, then you will have to create translation array (to translate number to color name) if u will need to use value as color name/slug. So in ACF settings choices:


  • 1:红色

  • 2:蓝色

  • 3:黄色

  • 4:白色

  • 5:黑色

  • 1 : RED
  • 2 : BLUE
  • 3 : YELLOW
  • 4 : WHITE
  • 5 : BLACK

查询参数将保持不变(除了类型排序DESC-> ASC),如果您需要从数字中获取名称,请在循环中使用此名称:

Query args will remain same (except type ordering DESC->ASC) and if you need to get the name from number, use this in loop:

$field = get_field_object( 'type' );
$value = $field['value'];
$color_name = $field['choices'][ $value ]; // this will be formated
$unformated_slug = sanitize_title( $color_name ); // change to lowercase and remove whitespaces etc..

// then you can work with $unformated_slug like your original field value eg:
if( $unformated_slug == 'red' ) {
    /* do something here */
}

将选择值更改为数字是最简单的方法,其他任何事情都会太复杂。

Changing choice values to numbers is the most simplier way, anything other will be too complicated.

-

默认发帖顺序为按日期。如果您要为所有查询自动设置订单或仅针对自定义帖子类型查询设置订单,请参见 https: //codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Default order of posts is by date. If you want to set your order automatically for all queries or only for custom post type queries, see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

这篇关于是否可以按分配的相同顺序查询帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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