最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - WP_Query Filtred by author name ( Return null )

programmeradmin1浏览0评论

I want to list all pages whose author has a specific name but WordPress returns null values

$args = array(
    'author_name '=> 'admin',
    'post_type'=>'page'
);

$pages = new WP_Query( $args );

foreach( $pages as $page ) {
    var_dump( $page->post_title );
}

result is

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL string(4) "test" NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

I want to list all pages whose author has a specific name but WordPress returns null values

$args = array(
    'author_name '=> 'admin',
    'post_type'=>'page'
);

$pages = new WP_Query( $args );

foreach( $pages as $page ) {
    var_dump( $page->post_title );
}

result is

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL string(4) "test" NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Share Improve this question edited Nov 4, 2020 at 15:22 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Nov 4, 2020 at 13:32 danial rafieedanial rafiee 1232 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

That's not how WP_Query works, it isn't what the documentation says either. WP_Query is not a function.

foreach needs an array, or something that can be iterated on, but you've given it a WP_Query object.

Instead, look at the documentation or tutorials, all of them follow this basic pattern for a standard post loop:

$args = [
    // parameters go here
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) { 
    while ( $query->have_posts() ) {
        $query->the_post();
        // display the post
        the_title();
        the_content();
    }
    wp_reset_postdata();
} else {
    echo "no posts were found";
}
发布评论

评论列表(0)

  1. 暂无评论