I have a site with more then 10.000 posts and images, its classipress site. I need somehow to show all images from posts that are on pending status, and who havent expired(post meta). This is the code I came up with and that is working:

function cb_dash_images(){
    $args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post_id, 'order' => 'ASC', 'orderby' => 'ID');
    $images = get_children($args);

    foreach($images as $image){ 
        // Get post expire time in timestamp
        $post_time = strtotime(get_post_meta($image->post_parent,'cp_sys_expire_date',true));
        // If draft and not expired
        if(get_post_status($image->ID) == 'pending' && current_time('timestamp') < $post_time) {
        $slika = wp_get_attachment_image($image->ID, 'thumbnail'); 
        edit_post_link( $slika, '<div class="dash-image" style="float:left;margin-right:5px; min-height:50px;">', '</div>', $image->post_parent ); 

This is working. BUT its going through all 35k images and check for each one of them, if its parrent -post status is on pending and if its parrent post meta have expired. And its really slow :) How can I first check if post is on pending and post meta value, then do the loop, as that way, it wouldnt go through 35k images, but only images from posts who are one pending and with specified post meta value.

Thank you!

  • What format is the date in your post meta saved? Commented Apr 8, 2013 at 2:07

1 Answer 1


I don't have the data to test so the code below may be wrong.

add_filter('posts_search', 'set_is_tax_to_true' ,10,2);
function set_is_tax_to_true($search,$query){
    $query->is_tax = true;
$args = array(
    'post_type' => 'attachment',
    'posts_per_page' => -1,
    'post_status' => 'pending',
    'post_parent' => $post_id,
    'order' => 'ASC',
    'orderby' => 'ID',
    'meta_query'=> array(
           'key' => 'cp_sys_expire_date',
           'value' => current_time('mysql'),
           'compare' => '>',
           'type' =>  'DATE'
$query = new WP_Query($args);
$images = $query->get_posts();

It'll generate SQL like SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts LEFT JOIN wp_posts AS p2 ON (wp_posts.post_parent = p2.ID) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'attachment' AND ((wp_posts.post_status = 'pending') OR (wp_posts.post_status = 'inherit' AND (p2.post_status = 'pending'))) AND ( (wp_postmeta.meta_key = 'cp_sys_expire_date' AND CAST(wp_postmeta.meta_value AS DATE) > '2013-04-08 10:11:09') ) GROUP BY wp_posts.ID ORDER BY wp_posts.ID

huh, very long SQL.

the set_is_tax_to_true hack is to force WP_Query to generate this ((wp_posts.post_status = 'pending') OR (wp_posts.post_status = 'inherit' AND (p2.post_status = 'pending')))

Note: I assume the date stored in post meta is in the format of 2012-12-12 12:12:12. If not, please follow this link to change the meta_query argument

  • Awesome. Its working, but just need to figure out date. Date is saved in this format: 04/25/2013 23:41:10 .I tried using 'meta_query'=> array( array( 'key' => strtotime('cp_sys_expire_date'), 'value' => current_time('timestamp'), 'compare' => '>', 'type' => 'DATE' ) but not sure if I can use strtotime there to compare as timestamp, as it does not work Commented Apr 8, 2013 at 7:57
  • ah got it, type shouldnt be date. Ok I got all results, now ill try with big database Commented Apr 8, 2013 at 8:00

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.