我想从Wordpress的媒体库中获取所有视频的列表.我是从常规WP循环之外做的,因此包括wp-load.php.但是,我收到了带有此代码的0个帖子(尽管实际上有4个视频):
include('../../../wp-load.php'); $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'video', 'numberposts' => -1, 'posts_per_page'=>-1, 'suppress_filters' => true, 'post_status' => 'any', 'post_parent' => null ); $attachments = get_posts( $args ); echo(count($attachments)); if ( $attachments ) { foreach ( $attachments as $post ) { setup_postdata( $post ); the_title(); echo('
'); echo wp_get_attachment_url( $post->ID, false ); echo('
'); } wp_reset_postdata(); }
但是,这个问题似乎与视频没有直接关系(或者只是):如果我'post_mime_type' => 'video'
从查询中删除了参数,我的媒体库中只返回了16个而不是121个附件(图像,声音,视频).我真的开始在这个问题上失去理智......
Heureka!经过几个小时的尝试几乎所有我找到了这个问题的原因:插件Polylang.
简而言之,如果在你的Wordpress安装中安装了Polylang,似乎必须为get_posts()函数提供另一个参数:它叫做lang!
它可以是一个空字符串,只列出所有资产,或者Polylang在特定Wordpress安装中使用的特定语言标记之一.所以最终工作的代码必须如下所示:
include('../../../wp-load.php'); $args = array('lang' => '', 'post_type' => 'attachment', 'post_mime_type' => 'video', 'numberposts' => -1, 'posts_per_page'=>-1, 'suppress_filters' => true, 'post_status' => 'any', 'post_parent' => null); $attachments = get_posts( $args ); echo(count($attachments)); if ( $attachments ) { foreach ( $attachments as $post ) { setup_postdata( $post ); the_title(); echo('<br />'); echo wp_get_attachment_url( $post->ID, false ); echo('<br />');echo('<br />'); } wp_reset_postdata(); }
我真的希望这可以帮助别人节省宝贵的时间!
此链接有助于找到我的解决方案:http: //polylang.wordpress.com/documentation/documentation-for-developers/general/