Show posts in select_advanced dropdown ordered by name

Support General Show posts in select_advanced dropdown ordered by name

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #7915
    clausgehrkeclausgehrke
    Participant

    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' ),
    ),
    #7922
    Anh TranAnh Tran
    Keymaster

    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?

    #7928
    clausgehrkeclausgehrke
    Participant

    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.

    #7935
    Anh TranAnh Tran
    Keymaster

    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?

    #7941
    clausgehrkeclausgehrke
    Participant

    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?

    #7946
    Anh TranAnh Tran
    Keymaster

    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.

    #7948
    clausgehrkeclausgehrke
    Participant

    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

    #7960
    Anh TranAnh Tran
    Keymaster

    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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Show posts in select_advanced dropdown ordered by name’ is closed to new replies.