Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Wordpress中的标题获取附件id?_Wordpress - Fatal编程技术网

如何从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)从来不会显示正在查询的标题列。