Support Forum
I tried to register a select_advanced field with a option-list of custom post types.
But the options are ordered by date i think.
I tried to set a orderby in the query_args but this doesn`t work.
is there a way to sort the order of options in the dropdown?
array(
'id' => $prefix . 'metabox-cpt',
'type' => 'post',
'name' => esc_html__( 'CPT', 'metabox' ),
'post_type' => 'cpt',
'query_args' => array(
'posts_per_page' => - 1,
'order' => 'ASC',
'orderby' => 'name',
),
'field_type' => 'select_advanced',
'clone' => true,
'sort_clone' => true,
'add_button' => esc_html__( 'add CPT', 'metabox' ),
),
Hi,
I've just checked again and the query returns the correct order. Here is what I have:
https://i.imgur.com/govniwQ.png
Do you run any code that hook to the query?
Hi Anh Tran,
Thanks for the fast reply.
I tested this again. It is working with the default WordPress blog posts as you show in the screenshot.
But there is a different order if you use a custom post type.
I've tested again with a custom post type "Book" and it works for me:
https://i.imgur.com/aYVSoZ0.png
Here is my code:
add_filter( 'rwmb_meta_boxes', 'your_prefix_post_demo' );
function your_prefix_post_demo( $meta_boxes ) {
$meta_boxes[] = array(
'title' => __( 'Post Field Demo', 'textdomain' ),
'post_types' => 'book',
'fields' => array(
array(
'name' => __( 'Post', 'textdomain' ),
'id' => 'post',
'type' => 'post',
'post_type' => array( 'book' ),
'field_type' => 'select_advanced',
'placeholder' => __( 'Select an Item', 'textdomain' ),
'query_args' => array(
'post_status' => 'publish',
'posts_per_page' => - 1,
'order' => 'ASC',
'orderby' => 'name',
),
),
),
);
return $meta_boxes;
}
Do you run any plugin that modifies the query? Such as WPML?
You are right the WPML-Plugin is part of the installation.
So would you recommend a custom Query to provide the data for options,
or is there another way to work around?
Please try adding a parameter ’suppress_filters’ => true
(or false
) to the query_args
. I read about this param somewhere but couldn’t find the correct post.
Oh I found my mistake.
I Used a string for the post_type setting.
'post_type' => 'cpt',
By Using an array everything is fine and options show up in correct order even without suppress_filters.
'post_type' => array( 'cpt' ),
Thanks a lot
That's a little bit weird cause post_type
can be either string or array. I will keep digging into this and see what happens.
Anyway, glad that it resolves your problem.
Merry Christmas.