Viewing 3 replies - 1 through 3 (of 3 total)
  • I have modified the plugin to integrate images included in galleries.

    In the get_posts_by_attachment_id function, just before the line

    $used_in_content = array_unique( $used_in_content );

    insert the following code :

    if ( wp_attachment_is_image( $attachment_id ) ) {
    	$search = new WP_Query( array(
    		's'        	=> '[gallery ids=',
    		'post_type'	=> 'any',
    		'nopaging'	=> true,
    	) );
    	if($search->have_posts()){
    		foreach($search->posts as $search_post) {
    			if(preg_match('/\[gallery ids="[\d,]*'.(string)$attachment_id.',[\d,]*"\]/', $search_post->post_content)){
    				$used_in_content[] = (string)$search_post->ID;
    			}
    		}
    	}
    }

    This should take pictures included in galleries into account.

    @Serguey : if you have a git repository for this plugin, I can send you a pull request.

    Nice one Fab1en,

    Your solution was much more elegant than mine!

    Here is his git: https://github.com/wp-plugins/find-posts-using-attachment

    I had created a initial function to make an array of ALL galleries from all posts on plugin load, than checked the array once each attachment ID was run.

    Main function, thought it might be lower overhead instead of running the database call every time.

    // Get all posts with galleries and thier IDs
    	   public $post_gallery_urls=array();
    
    		 function get_all_galleries_from_posts() {
    		 	$the_gallery_images = array();
    			$post_galleries_query = new WP_Query( array(
    					's'              => '[gallery',
    					'post_type'      => 'any',
    					'fields'         => 'ids',
    					'no_found_rows'  => true,
    					'posts_per_page' => -1,
    				) );
    				$theposts = $post_galleries_query->posts;
    				foreach($theposts as $post_gallery_id){
    					$galleries = get_post_gallery($post_gallery_id,false);
    					$this->post_gallery_urls[] =  array('post_id'=>$post_gallery_id,'images'=>explode(',',$galleries['ids']) );
    				};
    		}

    Checking if in array

    //Custom code to check for images in galleries
    
    		$used_in_gallery = array();
    		if ( wp_attachment_is_image( $attachment_id ) ) {
    			foreach ($this->post_gallery_urls as $galleries ) {
    				if(in_array( $attachment_id ,  $galleries['images'] )){
    			       $used_in_gallery[] = $galleries['post_id'];
    			   	}
    			}
    		}

    Than later on and updated the output to show “In Gallery” text.

    You are right Geet, caching the gallery search query as a class member would save some database access. Thanks for your comment.

    Regarding the git repository, the one you pointed is the WP plugins mirror. I don’t think Serguey can accept pull request on this one.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Lists pictures as unused altough they are used in a gallery’ is closed to new replies.