• Hi,

    What I’m trying to do is have a custom post type ordered differently on different pages. I’ve searched around for quite a while and can’t seem to find anything to help.

    I have a post type named staff which is a list of staff members. I then have several pages where those staff members are displayed using categories (a staff member can be in multiple categories and show up on multiple pages). I need to be able to order the staff list on a page by page basis.

    For example let’s say I have 6 staff in the custom post type.

    • – John
    • – Adam
    • – Steve
    • – Matt
    • – Andrew
    • – Tom

    In Category A there is John, Adam, Steve and Matt, in Category B there is John, Steve, Andrew and Adam.

    On Page A I want the staff members from Category A to be displayed but in the following order John, Adam, Steve and Matt on Page B the staff from Category B are to be displayed but in the order of Steve, Andrew. Adam and John. Therefore I can’t use menu_order.

    I thought the best way to accomplish this was to have a custom meta box on the Page edit screen where the posts can be ordered using jQuery.sortable.

    I have written a plugin which displays a list of the custom post type staff Plugin code below

    <?php
    
     /*
     Plugin Name: Staff Sort Meta Box
     */
    
     add_action('admin_print_scripts','plugin_init'); // plugin is any name
     function plugin_init() {
     /* Register our stylesheet. */
     wp_register_style( 'myPluginStylesheet', plugins_url('css/flaticon.css', __FILE__) );
     wp_enqueue_style( 'myPluginStylesheet' );
     /* Register jQuery. */
     wp_deregister_script('jquery');
     wp_register_script('jqua', plugins_url('js/jquery.js',__FILE__), false, '1.11.2');
     wp_enqueue_script('jqua');
     wp_deregister_script('jquery-ui-core');
     wp_deregister_script('jquery-ui-sortable');
     wp_deregister_script('jquery-ui-mouse');
     wp_register_script('jqueryui', plugins_url('js/jquery.min.js',__FILE__), false, '1.11.2');
     wp_enqueue_script('jqueryui');
     }
     add_action('admin_head','jui_admin_header'); //Add JQuery UI to the admin side
    
     function jui_admin_header() {
     ?>
     <script type="text/javascript">
     $(function() {
     $('.sortable').sortable({
        start: function(event, ui) {
            var start_pos = ui.item.index();
            ui.item.data('start_pos', start_pos);
        },
        update: function(event, ui) {
            var start_pos = ui.item.data('start_pos');
            var end_pos = ui.item.index();
            alert(start_pos + ' - ' + end_pos);
        }
    });
     });
     </script>
    
     <?php
     }
    
     /* Add a meta box to the page screen */
    
     function staff_meta_box() {
     add_meta_box( 'staff_meta', __( 'Staff Members', 'staff-textdomain' ), 'staff_meta_callback', 'page', 'normal', 'high' );
     }
    
     add_action( 'add_meta_boxes', 'staff_meta_box' );
    
     /* Output content of meta box */
     function staff_meta_callback( $post ) {
    
    /* Setup nonce */
    wp_nonce_field( basename( __FILE__ ), 'staffsort_nonce' );
    $staffsort_stored_meta = get_post_meta( $post->ID );
    
     /* Get the current Page ID */
     global $post;
     $pageID = $post->ID;
    
     if ( $pageID == 25) {
        $category = 'asset-advisory-services';
    }
     /* There will be a few other categories but I thought I'd save some reading for you */
    wp_reset_query();
    query_posts(array(
        'post_type' => 'staff',
        'categories' => $category
    ) );  
    
     echo '<ul class="sortable">';
     if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
    echo '<li>';
    echo '<i class="flaticon-move7"></i>';
    the_title();
    echo '</li>';
    
     endwhile; else:
     endif;
     echo '</ul>';
    
     }
     ?>

    This example http://so.devilmaycode.it/getting-the-position-of-the-element-in-a-list-when-its-drag-dropped-ui-sortable/ grabs the order and saves it to the menu_order which unfortunately I can’t use but wonder if it could be used to add the order information to postmeta.

    It would be good if I could store the order in the post_meta for each post but I’m not sure if this is possible as I’ll be editing a page and not the custom post types.

    Can you save postmeta information to a post when you save a page? If so, how would I go about saving it for each post that appears.

    If not I would have to add the sort order details to the post meta of the page I’m editing. Again how would I cycle through each custom post and save both the custom post ID and order for each one?

    Further to that I know how to order the WP Query with a meta_key but usually that is for the posts (hence why saving the info to the post would be great). Could I query the custom posts and order it by the postmeta values of a page if I put that into a page template?

    Or am I overcomplicating this?

  • The topic ‘Order posts differently on different pages’ is closed to new replies.