Get attached media only

时间:2014-03-28 作者:dev-jim

我正在使用wp\\u query获取上传的媒体。一切正常,但wp\\u query将返回未附加帖子的媒体。这不是我想要的。如何从wp\\U查询中排除未连接的介质?

这是我的查询参数,如下所示:

 $args = array(
  \'post_type\' => \'attachment\',
  \'post_status\' => \'inherit\',
  \'posts_per_page\' => -1,
  );
  $attachemnt = new WP_Query($args);
是否有任何参数可用于在wp\\U查询类中排除未连接的媒体?

谢谢

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

中的所有介质(有些不正确)$wpdb->posts 表将是“附件”,无论是否实际附加。实际附加的“附件”将具有post_parent 而不是0,因此您需要的是在post_parent 专栏,如果我理解你的话。

$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);

SO网友:Chetan
<?php if ( $post->post_type == **\'post type name\'** && $post->post_status == \'publish\' ) {
    $attachments = get_posts( array(
        \'post_type\' => \'attachment\',
        \'posts_per_page\' => -1,
        \'post_parent\' => $post->ID,
        \'exclude\'     => get_post_thumbnail_id()
    ) );

    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
            $thumbimg = wp_get_attachment_link( $attachment->ID, \'thumbnail-size\', true );
            echo \'<li class="\' . $class . \' data-design-thumbnail">\' . $thumbimg . \'</li>\';
        }

    }
}

?>

结束

相关推荐

Exclude Specific Categories?

我当前用于显示类别的代码是<?php foreach((get_the_category()) as $category) { echo $category->cat_name . \' \';}?>我想排除名为“home featured”的类别或ID为“65”的类别如何将其添加到上述代码中?