2014-09-09 4 views
0

이 작동하지 않습니다Wordpress의 제목에서 첨부 파일 ID를 가져 오는 방법은 무엇입니까?

$args = array( 
    "post_per_page" => 1, 
    "post_type" => "attachment", 
    "post_title" => trim("My Attachment") 
); 

//This is empty 
$get_posts = new WP_Query($args); 

나는 ID로 검색하면 post_title = "My Attachment"와 객체를 반환하기 때문에 그 첨부 파일이 해당 게시물의 제목으로 존재하는 것을 알고있다.

게시물 제목으로 첨부 파일을 찾으려면 어떻게해야합니까? 지연 죄송합니다

+0

http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks 더 내 경험에서 http://wordpress.stackexchange.com/questions/117069/how-to-find-attachment-by-its-name – ggdx

+0

을 참조 읽기, 당신은 할 수 없습니다 WP_Query에서 post_title을 사용하십시오. 나는 여러 번 시도했고, "$ query-> request"(생성 된 SQL 임)를 표시해도 제목 열을 조회하지 않는다. –

답변

2

... 당신은 클래스 WPDB에 직접 쿼리를 사용할 수 있습니다 ..

global $wpdb; 
$str= "My Attachment"; 
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_title = '$str' ", OBJECT); 
if ($posts){ 
    foreach ($posts as $post){ 
     echo $post->post_title; 
    } 
} 

을하지만 조심! SQL 인젝션 공격에서 코드를 보호 이