6

I have two custom post types (Branding Elements and Products) that I'm showing on a single page, and the client wants to be able to control the sort order of the page.

What would be the best course of action for creating a way for the client to re-order two post types with a drag n drop interface? Because they're 2 different post types, none of the currently available plugins are suitable.

3
  • You could create a meta field and then sort by that order using a meta_query. Drag and drop not necessarily possible without some work. You could deconstruct an existing reorder plugin to mention the two specific post types you are considering.
    – GhostToast
    Nov 1, 2013 at 18:26
  • 1
    You don't specify if this is front or backend. But IMO the Question is off-topic (help center) either because it is too broad (only writing a full blown plugin could answer it) and/or it is a recommendation question (off-topic in most Stack Exchange). That said, if it's backend, I was looking for this feature and found this one.
    – brasofilo
    Nov 1, 2013 at 18:35
  • If it's the back-end, just have your post type "support" page-attributes which will give it access to menu_order which you can then have you client sort by. Easily change-able in the Quick Edit section. Other than that, @brasofilo is correct, this is pretty broad and would need a plugin to handle it, tad off-topic.
    – Howdy_McGee
    Jan 27, 2015 at 20:50

3 Answers 3

6

It's common to utilize the WordPress navigational menu UI to get the drag/drop feature for free.

I think there are already some answers, on this site, explaining this better than I can, but I can't seem to find them for you. So let my try to construct one simple example:

  1. We go to Appearance/Menus and create a menu with e.g. the mylist menu id.

  2. Then we drag/drop posts onto that menu and save it.

  3. Add the following shortcode to a page/post content:

    [wpd_list menu_id="mylist"]
    

with the following code snippet to support it:

/**
 * Shortcode [wpd_list] to display posts ordered by the nav menu UI
 */
add_shortcode( 'wpd_list', function( $atts = array(), $content = null )
{
    $atts  = shortcode_atts( 
        array( 'menu_id' => '', ), 
        $atts, 
        'shortcode_wpd_list' 
    );
    $items = wp_get_nav_menu_items( sanitize_key( $atts['menu_id'] ) );
    $ids   = wp_list_pluck( $items, 'object_id' );   // Don't use db_id or page_name!
    $li    = '';
    $q     = new WP_Query( 
        array( 
            'post_type' => 'any', 
            'post__in'  => (array) $ids,
            'orderby'   => 'post__in',         // Must have for the correct order!
        ) 
    );
    while( $q->have_posts() )
    { 
        $q->the_post();
        $li .= sprintf( 
            '<li><h2><a href="%s">%s</a></h2></li>',    // Modify this HTML to your needs!
             get_permalink(), 
             get_the_title() 
        );
    }
    wp_reset_postdata();
    return ! empty( $li ) ? sprintf( '<ul>%s</ul>', $li ) : '';
} );

PS: When writing this example I did at least two mistakes at first ;-)

I first forgot to fetch the posts in the posts__in order, to match the nav menu order.

Secondly I used the db_id as the post ID's instead, of the object_id field.

After fixing that, this seemed to run as expected, so I just post it here now ;-)

2

This isn't going to work with your current setup. You need to create a single post type and split the posts via a custom taxonomy which would have two terms: 'Branding Elements' and 'Products'. This is the only way to realistically combine two post types into one manageable list that can be reordered by drag-and-drop.

From there you can display the order via 'order' => 'ASC', 'orderby' => 'menu_order' in your query $args and use Simple Page Ordering to drag-and-drop them in the dashboard.

Also, when registering your post type you'll need to include 'hierarchical' => 'page-attributes'. So that the posts can be reordered.

1
  • 2
    I agree. People tend to use multiple custom post types when what they're trying to achieve would be a lot easier to achvieve by using custom taxonomies. Jan 29, 2015 at 10:38
0

Use categories.

Make a category that you can apply to both post types, and then use this plugin:

https://wordpress.org/plugins/custom-post-order-category/

To order all posts in that category.

Don't show the category on the front end of your website, as its purpose is purely to help relate the two post types for this plugin to use.

4
  • Thanks a lot for the hint! I already installed the plugin but it only lets me display the post of one post type at the same time. Jan 27, 2015 at 22:54
  • Ah, and my categories won’t display in the dropdown. I can only choose from my post types. Jan 27, 2015 at 22:59
  • Okay, I managed that I can select my order category. When I click on »Load Posts« it apparently lists all the posts with that category but the titles of posts from a different post type than post are not displayed. Jan 27, 2015 at 23:27
  • Conclusion: Good idea but with this plugin it doesn’t work out — unfortunately too buggy … Jan 28, 2015 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.