1

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
  • BLUE
  • YELLOW
  • WHITE
  • BLACK

Only one can be selected from the above.

The below $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',
      ),
  );

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.

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.

EDIT 1 : Something like this

Community
  • 1
  • 1
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37

1 Answers1

0

UPDATE:

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
  • BLUE
  • YELLOW
  • WHITE
  • BLACK

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).

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 : RED
  • 2 : BLUE
  • 3 : YELLOW
  • 4 : WHITE
  • 5 : BLACK

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.

--

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

keyBeatz
  • 139
  • 6
  • That's good to know about default ordering, can't accept this as an answer though. My problem is irrelevant to your answer. – Antonios Tsimourtos Feb 21 '17 at 15:49
  • Please tell me what order you are trying to achieve. Orderby uses its array content in priority thus first declaration is used first and then second etc. – keyBeatz Feb 21 '17 at 15:52
  • I don't want it to order it ASC by alphabetical order but ASC by "assigned" order. By assigned i mean as the list i mentioned. First(RED), Second(BLUE), Third(YELLOW).. etc . Check my question on bottom, found something similar. – Antonios Tsimourtos Feb 21 '17 at 16:02
  • You got me confused the way you said it. Isn't it possible by editing the query of my question? By ascending of that fields number? – Antonios Tsimourtos Feb 28 '17 at 11:38
  • I think it isn't possible the way you think. But you could try to use ACF field naming, if it is created in ascending order you might give it try. Look at db for your postmeta table and see every registered custom field have another field with "_" prefix registered. I don't know how this code is generated and if it is in format that might be used for ordering, you have to try it. – keyBeatz Feb 28 '17 at 13:12
  • From what i understand this `'type_clause' => array( 'key' => 'type', ),` reads the field ("RED") not value ("1") right? – Antonios Tsimourtos Feb 28 '17 at 13:46
  • Okay got it after a lot of "mind burning". Thanks a lot! – Antonios Tsimourtos Feb 28 '17 at 14:20