Support » Plugin: Elementor Page Builder » Only images attached to post in Media Library

  • Hi –

    First of all: Very cool plugin!

    Currently evaluating it but found a (in my case) big showstopper: If i want to select an image (or multiple) for a image- or gallery-widget, i always see all images uploaded and not only those uploaded to this post/cpt.

    Since i have a lot of images in my usecase, this is a big pita everytime i want to choose an image: Lots of unwanted request going to my server in the background.

    Is that your plugins default behaviour, or only on my site?

    A feature request if default:

    Please implement a global configuration option where i can choose what i want to see if i choose images.

    Thanks and best regards from

    Salzburg, Austria!

Viewing 2 replies - 1 through 2 (of 2 total)
  • I found a rather ugly solution: By modifying the wp_query for the wp media selector (pre_get_posts).

    Unfortunately, Elementor doesn’t hand over the post id when calling the wp attachment selector (get variable post_id contains 0), so i’m fetching it via the http referer, which is not very reliable.

    This solution may not work in every browser:

    add_action('pre_get_posts', function($query) {
      global $pagenow;
    
      if ('admin-ajax.php' != $pagenow) {
        return;
      }
      
      if ($_REQUEST['action'] != 'query-attachments') {
        return;
      }
      
      // Elementor doesn't provide the post_id when calling
      // the wp media selector: We have to try to get it 
      // via the http referer (ugly). Additionally we
      // prevent the code to be executed in other locations.
    
      $referer = $_SERVER['HTTP_REFERER'];
      
      if (strpos($referer, 'elementor') === false) {
        return;
      }
      
      preg_match('/post=([0-9]+)/i', $referer, $matches);
      $post_id = $matches[1];
    
      // Set the filter
      
      $query->set('post_parent', $post_id);
    
      return;
      
    });

    Ah nope – unfortunately doesn’t work either: If calling the media library without post_id, uploaded files there don’t get set the correct parent_id.

    Could you please hand over the post_id when calling it?

    Many thanks in advance
    and best regards from Salzburg, Austria.

    – Johannes

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Only images attached to post in Media Library’ is closed to new replies.