Skip to content

Instantly share code, notes, and snippets.

@bordoni
Created October 4, 2012 22:23
Show Gist options
  • Select an option

  • Save bordoni/3836854 to your computer and use it in GitHub Desktop.

Select an option

Save bordoni/3836854 to your computer and use it in GitHub Desktop.
Query posts with thumbnail or any attachments within a certain category
<?php
global $wpdb;
$possible_ids = $wpdb->get_col($wpdb->prepare("SELECT `post_parent` FROM {$wpdb->$posts} WHERE `post_parent`!=0 AND `post_type`='attachment' GROUP BY `post_parent`;"));
$args = array(
'post__in' => $possible_ids,
'posts_per_page' => 4,
'category__in' => array(8),
'meta_query' => array(
array(
'key' => '_thumbnail_id'
),
)
);
$q = WP_Query($args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment