Skip to main content
Post Undeleted by s_ha_dum
Post Deleted by s_ha_dum
deleted 345 characters in body
Source Link
s_ha_dum
  • 65.5k
  • 13
  • 84
  • 174

All media (somewhat incorrectly) in the $wpdb->posts table will be "attachments" whether actually attached or not. "Attachments" that are actually attached will have a post_parent other than 0, so what you need are all of the attachments that have a 0 in the post_parent column, if I understand you. WP_Query can't do that natively, but a relatively simple filter should make it work.

function filter_post_parent_wpse_139516($where) {
  remove_filter('posts_where','filter_post_parent_wpse_139516');
  global $wpdb;
  $where .= " AND {$wpdb->posts}.post_parent != 0";
  return $where;
}
add_filter('posts_where','filter_post_parent_wpse_139516');

$args = array(
  'post_type' => 'attachment',
  'post_status' => 'inherit',
  'posts_per_page' => -1,
  'post_parent__not_in' = array(0)
);
$attachment = new WP_Query($args);
  
var_dump($attachment->posts);

All media (somewhat incorrectly) in the $wpdb->posts table will be "attachments" whether actually attached or not. "Attachments" that are actually attached will have a post_parent other than 0, so what you need are all of the attachments that have a 0 in the post_parent column, if I understand you. WP_Query can't do that natively, but a relatively simple filter should make it work.

function filter_post_parent_wpse_139516($where) {
  remove_filter('posts_where','filter_post_parent_wpse_139516');
  global $wpdb;
  $where .= " AND {$wpdb->posts}.post_parent != 0";
  return $where;
}
add_filter('posts_where','filter_post_parent_wpse_139516');

$args = array(
  'post_type' => 'attachment',
  'post_status' => 'inherit',
  'posts_per_page' => -1,
);
$attachment = new WP_Query($args);
  
var_dump($attachment->posts);

All media (somewhat incorrectly) in the $wpdb->posts table will be "attachments" whether actually attached or not. "Attachments" that are actually attached will have a post_parent other than 0, so what you need are all of the attachments that have a 0 in the post_parent column, if I understand you.

$args = array(
  'post_type' => 'attachment',
  'post_status' => 'inherit',
  'posts_per_page' => -1,
  'post_parent__not_in' = array(0)
);
$attachment = new WP_Query($args);
  
var_dump($attachment->posts);
Source Link
s_ha_dum
  • 65.5k
  • 13
  • 84
  • 174

All media (somewhat incorrectly) in the $wpdb->posts table will be "attachments" whether actually attached or not. "Attachments" that are actually attached will have a post_parent other than 0, so what you need are all of the attachments that have a 0 in the post_parent column, if I understand you. WP_Query can't do that natively, but a relatively simple filter should make it work.

function filter_post_parent_wpse_139516($where) {
  remove_filter('posts_where','filter_post_parent_wpse_139516');
  global $wpdb;
  $where .= " AND {$wpdb->posts}.post_parent != 0";
  return $where;
}
add_filter('posts_where','filter_post_parent_wpse_139516');

$args = array(
  'post_type' => 'attachment',
  'post_status' => 'inherit',
  'posts_per_page' => -1,
);
$attachment = new WP_Query($args);
  
var_dump($attachment->posts);