如何从Wordpress中的标题获取附件id?
这不起作用:如何从Wordpress中的标题获取附件id?,wordpress,Wordpress,这不起作用: $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\u title=“我的附件”的对象 我怎样才
$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\u title=“我的附件”
的对象
我怎样才能通过文章标题找到附件?很抱歉耽搁了。。。
您可以使用直接查询类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注入攻击
请阅读中的更多内容根据我的经验,您不能在WP_查询中使用post_标题。我已经尝试过很多次了,显示“$query->request”(这是生成的SQL)从来不会显示正在查询的标题列。