Support

Account

Home Forums Feature Requests Speed up work with new option in gallery field

Solved

Speed up work with new option in gallery field

  • Hi, I am uploading images with default wordpress drag and drop method. Witch large screen space, it’s faster and easier. With my old theme, I just uploaded images, closed the upload dialogue window, marked checkbox “show slideshow” and it was done.

    Going to a new theme with ACF, I have lots of post with images, that needs to be migrated to acf gallery field. But the process is quite slow. Currently it takes 6 steps to add post images to gallery field.

    1. click “add to gallery”
    2. click “all media items”
    3. pick “uploaded to this post”
    4. click first image
    5. ctrlshift click last image
    6. click “select”

    But it could be like this if you add one option

    1. click “Attach all post images”

    Is it possible?

  • It I

    1) add to gallery
    2) drag all my images to the media library
    3) all of the images I’ve uploaded are checked click select

  • I understand that it takes 3 steps with your method when uploading images to a new post (or with mine suggested).

    But I need to fix few hundred old posts manually (asi I understood from reading through forum that there is no simple method to updating them).

    Multiplying that number by 6 hurts.

  • If I had a few hundred posts to update I would more than likely build php cron task that interacted directly with the database.

    next best solutions would be to create an acf/load_value filter https://www.advancedcustomfields.com/resources/acfload_value/. If the value === NULL then no value is set, get all of the image IDs uploaded to the post. This second method would mean you don’t need to update anything unless you want to change the gallery.

  • Thank you for trying to help me. Acf/load_value filter looks like feasible option. Unfortunately I don’t understand php.

  • 
    add_filter('acf/load_value/name=gallery_field_name', 'bob4bob_load_gallery_from_post', 10, 3);
    function bob4bob_load_gallery_from_post($value, $post_id, $field) {
      if (!value !== NULL) {
        // field has a value, don't change it
        return $value;
      }
      $images = get_attached_media('image', $post_id);
      if (count($images)) {
        $value = array();
        foreach ($images as $image) {
          $value[] = $image->ID;
        }
      }
      return $value;
    }
    
  • I tried to put it in gallery.php, and functions.php, but suppose problem might lie here

    <?php
    
    add_filter( 'post_gallery', 'authentic_post_gallery', 10, 3 );
    
    function authentic_post_gallery( $output = '', $atts, $instance ) {
    
      $post = get_post();
    
      $default = array(
        'id'         => $post ? $post->ID : 0,
        'order'      => 'ASC',
        'orderby'    => 'menu_order ID',
        'columns'    => 3,
        'size'       => 'thumbnail',
      );
    
      $atts = array_merge($default, $atts);
    
      $id = intval( $atts['id'] );
    
      if ( ! empty( $atts['include'] ) ) {
        $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
    
        $attachments = array();
        foreach ( $_attachments as $key => $val ) {
          $attachments[$val->ID] = $_attachments[$key];
        }
      } elseif ( ! empty( $atts['exclude'] ) ) {
        $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
      } else {
        $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
      }
    
      if ( empty( $attachments ) ) {
        return '';
      }
    
      $selector = "gallery-{$instance}";
    
      if (isset($atts['layout'])) {
    
        $layout = $atts['layout'];
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Speed up work with new option in gallery field’ is closed to new replies.