Alphabetize checkbox_list field type

Support MB Builder Alphabetize checkbox_list field typeResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #32898
    mschraedermschraeder
    Participant

    I have a custom post type, "Technical Documents", and have all these posts listed using a checkbox_list field type for the user to select which posts to display. However, there are 100+ posts and it would be easier to use if they were in alphabetical order. Is there a way to do this?

    Thank you!

    My code:

    $meta_boxes[] = [
            'title'   => esc_html__( 'Technical PDFs to Display', 'online-generator' ),
            'id'      => 'selectpdf',
        'post_types' => ['product'],
            'context' => 'normal',
    'fields'  => [
        [
            'type'       => 'post',
            'name'       => esc_html__( 'Select Product PDFs', 'online-generator' ),
            'desc'       => 'online-generator',
            'id'         => $prefix . 'product_pdfs',
            'post_type'  => 'technicaldocuments',
            'field_type' => 'checkbox_list'
        ],
    ],
    ];
    #32903
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the setting query_args to pass the arguments to the post query to order posts by alphabetical. For example:

            'fields' => [
                [
                    'name'       => __( 'Post', 'your-text-domain' ),
                    'id'         => $prefix . 'post_kyojjvjwxa',
                    'type'       => 'post',
                    'post_type'  => ['post'],
                    'field_type' => 'checkbox_list',
                    'query_args' => [
                        'order' => 'ASC',
                    ],
                ],
            ],

    Please get more details on the documentation
    https://docs.metabox.io/fields/post/#sample-code
    https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

    #32910
    mschraedermschraeder
    Participant

    I added 'orderby'=> 'title', in the 'query_args' and it worked perfectly! Thanks for the quick help.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.