Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:00
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save aliciaduffy/11258457 to your computer and use it in GitHub Desktop.
WordPress: Show all posts containing media/attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Supporting script to load posts containing attachement. | |
* Using AJAX because query slows down page load. | |
*/ | |
function duffy_register_ajax_load_related() { | |
$current_screen = get_current_screen(); | |
if ( !is_null($current_screen) && $current_screen->base == 'post' && $current_screen->post_type == 'attachment' ) { | |
// javascript directory variables | |
$js = plugin_dir_url(__FILE__) . 'js/'; | |
// register scripts | |
wp_register_script( 'ajax-load-related', $js . 'load-related.js', array( 'jquery' ) ); | |
wp_localize_script( 'ajax-load-related', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); | |
wp_enqueue_script( 'jquery' ); | |
wp_enqueue_script( 'ajax-load-related' ); | |
} | |
} | |
add_action( 'admin_enqueue_scripts', 'duffy_register_ajax_load_related' ); | |
/** | |
* Create metabox to list posts containing attachement, initially empty | |
*/ | |
function duffy_list_attachment_posts_meta() { | |
add_meta_box( 'list-attachment-posts-meta', | |
'Related Posts', | |
'duffy_list_attachment_posts_callback', | |
'attachment', | |
'side', | |
'low' | |
); | |
} | |
add_action( 'admin_init', 'duffy_list_attachment_posts_meta' ); | |
/* Meta box initially contains post ID to pass to ajax function */ | |
function duffy_list_attachment_posts_callback() { | |
global $post; | |
echo '<span id="' . $post->ID .'"></span>'; | |
} | |
/** | |
* AJAX function to list all posts referencing/containing the attachment | |
*/ | |
function duffy_load_related() { | |
global $post, $all_post_types; | |
$postID = (isset($_GET['postid'])) ? $_GET['postid'] : 0; | |
// get attachment metadata | |
$metadata = wp_get_attachment_metadata($postID); | |
$filename = $metadata[file]; | |
// remove the last 4 characters from file (extension and . - '.jpg' or '.gif') | |
$filename = substr($filename, 0, -4); | |
// query for all posts containing that file (or resized variations) | |
$search_args = array( | |
'post_type' => $all_post_types, | |
'numberposts' => 500, | |
's' => $filename | |
); | |
if ($filename != '') { | |
$posts_containing_image = get_posts($search_args); | |
} | |
// Posts that contain the image in the body content ?> | |
<h4 style="margin-bottom: 0;">Posts Containing Image:</h4> | |
<?php if ($posts_containing_image) { ?> | |
<ul style="margin-top: 0;"> | |
<?php foreach ( $posts_containing_image as $post ) : setup_postdata( $post ); ?> | |
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> | |
<?php endforeach; ?> | |
</ul> | |
<?php } else { ?> | |
<p style="margin-top: 0;">None found.</p> | |
<?php } | |
// Posts that use this image as featured image | |
$values_serialized = array($postID); | |
$featured_args = array( | |
'post_type' => $all_post_types, | |
'meta_query' => array ( | |
array( | |
'key' => '_thumbnail_id', | |
'value' => $values_serialized, | |
'compare' => 'IN' | |
) | |
), | |
'numberposts' => 500 | |
); | |
$posts_featured_image = get_posts($featured_args); ?> | |
<h4 style="margin-bottom: 0;">Posts Featuring Image:</h4> | |
<?php if ($posts_featured_image) { ?> | |
<ul style="margin-top: 0;"> | |
<?php foreach ( $posts_featured_image as $post ) : setup_postdata( $post ); ?> | |
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> | |
<?php endforeach; ?> | |
</ul> | |
<?php | |
} else { ?> | |
<p style="margin-top: 0;">None found.</p> | |
<?php } | |
die(); | |
} | |
add_action("wp_ajax_duffy_load_related", "duffy_load_related"); | |
add_action("wp_ajax_nopriv_duffy_load_related", "duffy_load_related"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* List posts containing the attachment */ | |
jQuery(function($){ | |
var loading = true; | |
var postid = $('#list-attachment-posts-meta .inside span').attr('id'); | |
var $content = $("#list-attachment-posts-meta .inside"); | |
var load_related_posts = function(){ | |
$.ajax({ | |
type : "GET", | |
data : {action: "duffy_load_related", "postid": postid}, | |
dataType : "html", | |
url : myAjax.ajaxurl, | |
beforeSend : function(){ | |
$content.append('<img id="temp_load" src="ajax-loader.gif" style="display: block; margin: 10px auto;" />'); | |
}, | |
success : function(data) { | |
$data = $(data); | |
if ( $data.length ){ | |
$data.hide(); | |
$content.append($data); | |
$("#temp_load").remove(); | |
loading = false; | |
$data.fadeIn(500); | |
} else { | |
$("#temp_load").remove(); | |
} | |
}, | |
error : function(jqXHR, textStatus, errorThrown) { | |
$("#temp_load").remove(); | |
alert(jqXHR + " :: " + textStatus + " :: " + errorThrown); | |
} | |
}); | |
} | |
load_related_posts(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment