2014-11-06 4 views
0

the types plugin의 특정 사용자 정의 필드가있는 모든 이미지를 true으로 설정하려고합니다. 그것은 또한 post_content 또는 post_excerpt에 의해 그들을 걸러 낼 것이다. 그러나 나의 시도의 어느 것도 지금까지 일하지 않았다. 단지 allthough사용자 정의 필드로 이미지 가져 오기

<? 
    $args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'post_content' => 'foo', 
    'numberposts' => -1 
); 
?> 

<? print_r(get_posts($args)); ?> 

이 GET의 모든 이미지 는 POST_CONTENT foo 있습니다. WP_Query을 사용하려는 시도도 비참하게 실패했습니다.

도움을 주시면 감사하겠습니다.

+0

코드가 모든 이미지를 반환하는 이유는'get_posts'에'post_content' 매개 변수가 없기 때문입니다. http://codex.wordpress.org/Function_Reference/get_posts –

답변

0

WP_Query 방법 :

$args = array(
    'post_type' => 'attachment', 
    'post_status' => 'inherit', 
    'meta_query' => array(
     array(
      'key'  => 'color', 
      'value' => 'blue',   
      'compare' => 'LIKE', 
     ), 
    ), 
); 
$query = new WP_Query($args); 

난 당신이 WP_Query에 실패하는 이유는 다음의 조건에 의한 것으로 추정하고있다.

Codex 상태 : 기본 WP_Query는 'post_status'=> 'publish'로 설정하지만 첨부 파일의 기본값은 'post_status'=> 'inherit'이므로 post_status를 'inherit'또는 'any' 게다가.

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

get_posts 방법 :이 방법은 정확하게에 meta_value 요구에 사용자 정의 필드에 입력 한 내용과 일치한다는 것입니다 함께

$args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'meta_key' => 'custom-field', 
    'meta_value' => 'custom value', 
    'numberposts' => -1 
); 

print_r(get_posts($args)); 

는 다시 그립니다. get_posts을 계속 사용하려면 위의 WP_Query 예와 같이 meta_query을 사용하십시오.

+0

감사합니다. . 당신이 아직 영웅이 아닌 영웅 배지를 가지고 있지 않기 때문에 나는 upvote하지 않을 것입니다 :) –

+0

고마워, 내가 도울 수있어서 기쁩니다 :) –

관련 문제