Support » Alpha/Beta/RC » 3.5-rc1 menu_order not saved to the database for attached images

  • I am trying to re-order the images attached to a CPT and use do_shortcode(‘[gallery orderby="menu_order" exclude="' . get_post_thumbnail_id( $post->ID ) . '" columns="4"]‘) to display the gallery after all other post meta is displayed.

    When I insert the gallery into the content ‘field’, the ordering works. However it is not due to menu_order being saved. I think it is using the ids attribute to determine order.
    IE: [gallery columns="4" link="file" ids="1312,1301,1302,1304,1308,1309,1307,1310,1298,1299,1306,1311,1300,1297,1296,1303"]

Viewing 15 replies - 1 through 15 (of 15 total)
  • I’m not entirely sure what you are reporting. However, yes, “ids” is a comma-separated set of IDs, in the order in which they should appear. menu_order is no longer used.

    Thread Starter Nowell VanHoesen


    I was trying to use the menu_order to display the gallery outside of the content.

    Is there another way to get the order I want without having to insert the gallery into the content field?

    Galleries can be images uploaded to any post. They no longer need to be just the images uploaded to that post. And, it’s also so easy to build your own gallery (the order, what you’ve included, etc.). So menu_order and include didn’t really cut it. That’s where “ids” came in.

    I don’t quite know how to answer your question, which is a good one. In 3.5, “Gallery” was more or less tied to the post overall. It was a way to “manage” all of the images attached to a post. (I say that in quotes because it was really painful and not very useful.) We’ve sacrificed that to steer galleries towards being a loose collection of images embedded into a post.

    We could potentially continue to save menu_order under the hood for images that are attached to the post. That would reduce potential problems, for sure.

    Thread Starter Nowell VanHoesen


    Nacin, thanks for the info.

    Maybe a hook for an action would be helpful.

    Besides using wp_editor(), is there a way to add the “Add Media” button to a meta box?

    @nacin and @nowell, that’s exactly the issue I tried to describe in this lengthy post:

    Notes on the WordPress 3.5 Media Uploader

    Now, I have no idea how many theme authors have been using the menu_order to build galleries that were displayed outside of the main content field, but I think it was a smart and powerful solution.

    It was actually a CMS feature, that gave a lot of freedom to designers of custom themes. Now if the menu_order would become deprecated, and we would have to rely on the ID order written inside the shorttag inside the post content, we would lose that freedom. It would be a step back to a more conventional “blog layout” way of thinking.

    I’m still looking for a way to get the image reordering working with 3.5…

    Amy, thanks a lot for the link! This makes my day šŸ™‚

    Thread Starter Nowell VanHoesen


    Outstanding! Thank you! Glad to hear it’s back @sabreuse.

    @manuel – The way I got around it was to create a stripped wp_editor and have add the gallery to it. I would save this field in meta to be used where I want.

    the editor:
    wp_editor( $gallery, 'gallery', array( 'textarea_rows' => 0, 'wpautop' => false, 'tinymce' => false, 'quicktags' => false ) );

    the save:
    update_post_meta( $reviewID, 'gallery', $_POST['gallery'] );

    the display:
    do_shortcode( $gallery );

    Granted, the save needs some validation work to only allow the gallery shortcode, but I think you see the way around not having menu_order.

    Another option I was exploring involved using the gallery field type from Advanced Custom Fields.

    @nowell – Thanks for those explanations.

    With this method, using the shortcode, could you easily extract the first image and display it with the “Medium” size, and show the rest as “Thumbs”?

    Or loop through all the images in a gallery and retrieve the ones that are >= than a given amount of pixels?

    Thread Starter Nowell VanHoesen


    When you use the new media manager, you can specify which images attached to the ‘post’ to use in the gallery. You don’t have to select the one used for the Featured Image.

    Now that menu_order is back, you can do something like this:

    $galShortcode = '[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"  columns="4"]';
    echo do_shortcode( $galShortcode );

    Since the default order was by menu_order it soul be in the order you put them in.

    Indeed, that’s an interesting technique – wouldn’t ever have thought of that.

    I used the following to maintain the order for new and old galleries

    function pmc_gallery_menu_order_fix($id) {
            $regex_pattern = get_shortcode_regex();
            preg_match ('/'.$regex_pattern.'/s', stripslashes($_POST['content']), $regex_matches);
            if ($regex_matches[2] == 'gallery') :
                $attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
                $attribureStr = str_replace ('"', '', $attribureStr);
                $attributes = wp_parse_args ($attribureStr);
            $ids = explode(',', $attributes[ids]);
            $images = get_posts( array(
    		'post_parent' => $post->ID,
    		'numberposts' => '-1',
    		'post_status' => 'inherit',
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image',
    		'orderby' => 'menu_order ID',
    		'order' => 'ASC'
    	) );
    	if ( empty($images) ) {
    		// no attachments here
    	} else {
    		foreach ( $images as $attachment_id => $attachment ) {
    			if (in_array($attachment->ID, $ids)) {
    				$update_post = array();
    				$update_post['ID'] = $attachment->ID;
    				$update_post['menu_order'] = array_search($attachment->ID, $ids);
    				wp_update_post( $update_post );
    add_action('pre_post_update', 'pmc_gallery_menu_order_fix');

    Thank you so much for that fix!


    Hello. Thank you for your work on this issue.

    Could you please clarify for me the use of the code you provided? I added it to my plugin but I am not seeing an effect on the menu_order of subsequently uploaded images.

    What behavior should I expect to see if it is working? The automatic assignment of a menu_order?

    Thank you for your time.

    WordPress 3.5 is not longer in beta but has been released.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘3.5-rc1 menu_order not saved to the database for attached images’ is closed to new replies.