Orderby and Sort Order on frontend for Post Checkbox List.

Support MB User Meta Orderby and Sort Order on frontend for Post Checkbox List.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5205
    martinschapiromartinschapiro
    Participant

    I'm using a checkbox list field-type to allow the user to select multiple posts on the backend. I'm able to manipulate the appearance order for this backend view, however I unsure how to control the order in which posts are returned on the frontend, in my template.

    The code I'm currently using, below, does not seem to respond when I change the menu_order of the posts. Specifically, the backend view changes to reflect the new post sort order, but the frontend view remains the same.

    <?php $posts_array = rwmb_meta('_posts_select');?>

    Are there arguments, similar to those below, I can use to sort posts into the returned array?

    <?php $posts_array = rwmb_meta('_posts_select', 'sort_order=ASC&orderby=menu_order');?>

    Thanks in advance for your time looking at this.

    #5210
    Anh TranAnh Tran
    Keymaster

    The menu_order is used by WP_Query which has effect only when you use a custom query.

    Because the rwmb_meta function only returns the values stored in the post meta, it doesn't use the WP_Query and thus doesn't have the menu_order affected. However, after you change the post order, IF you edit the post and save the post meta again, then the new values might have the new order affected.

    In general, if you want to make sure it always work, you should use a custom query like this:

    $posts_array = rwmb_meta( '_posts_select' );
    $query = new WP_Query( array(
        'post_type' => 'post',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__in' => $posts_array,
    ) );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) : $query->the_post();
            // Do something
        endwhile;
    }
    #5214
    martinschapiromartinschapiro
    Participant

    Thank you so much. This is a great solution.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Orderby and Sort Order on frontend for Post Checkbox List.’ is closed to new replies.