I currently use the pmc_gallery_menu_order_fix for setting the menu order for image attachments in galleries as seen here: Wordpress 3.5 Gallery Menu Order not set?

With Wordpress 4.0, I am now getting an endless loop on the wp_update_post part of the code. Breaking it down, there appears the post IDs, arrays etc all seem to work properly but when it hits that update post seems to get wacky. Running into a out of memory error due to the endless loop it appears.

Does anyone have another solution to update the menu_order for attachments from gallery shortcode?

EDIT: the answer I used from the original question referenced above is - https://wordpress.stackexchange.com/a/89370/57313


1 Answer 1


What I found to work for me was to remove the action within the foreach ending with the following code

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);
  //this remove actions breaks the loop
            remove_action('pre_post_update', 'pmc_gallery_menu_order_fix');
            wp_update_post( $update_post );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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