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

wp query - new WP_Query with author argument return no results

programmeradmin1浏览0评论

I am trying to retrieve custom post types by author with no success.

Registered post type:

register_post_type( 'resume', array(
    'capability_type'   => 'resume',
    'map_meta_cap'      => true,
    'show_in_rest'      => true,
    'supports'          => array( 'title', 'editor', 'comments', 'author', 'revisions' ),
    'rewrite'           => array( 'slug' => 'resume' ),
    'has_archive'       => true,
    'public'            => true,
    'menu_icon'         => 'dashicons-welcome-write-blog'
    'labels' => array(
        'name'          => 'Resumes',
        'add_new_item'  => 'Add New Resume',
        'edit_item'     => 'Edit Resume',
        'all_items'     => 'All Resumes',
        'singular_name' => 'Resume'
    ),
) );

I get all entries with:

new WP_Query( array(
    'post_type' => 'resume'
) );

But no results with:

new WP_Query( array(
    'post_type' => 'resume',
    'author'    => 1,
) );

What am I doing wrong?

I am trying to retrieve custom post types by author with no success.

Registered post type:

register_post_type( 'resume', array(
    'capability_type'   => 'resume',
    'map_meta_cap'      => true,
    'show_in_rest'      => true,
    'supports'          => array( 'title', 'editor', 'comments', 'author', 'revisions' ),
    'rewrite'           => array( 'slug' => 'resume' ),
    'has_archive'       => true,
    'public'            => true,
    'menu_icon'         => 'dashicons-welcome-write-blog'
    'labels' => array(
        'name'          => 'Resumes',
        'add_new_item'  => 'Add New Resume',
        'edit_item'     => 'Edit Resume',
        'all_items'     => 'All Resumes',
        'singular_name' => 'Resume'
    ),
) );

I get all entries with:

new WP_Query( array(
    'post_type' => 'resume'
) );

But no results with:

new WP_Query( array(
    'post_type' => 'resume',
    'author'    => 1,
) );

What am I doing wrong?

Share Improve this question edited Jul 31, 2019 at 14:37 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Jul 31, 2019 at 1:31 www.mateminglerwww.matemingler 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Whenever you use capability_type you need to then assign those capabilities to the roles which should have access to the post type; Otherwise they will not have access to the post type.

Awhile back Justin Tadlock wrote an article around capbility_type: Meta Capabilities For Custom Post Types.

If you don't want to have to assign these capabilities to roles then you can simply change it to 'capability_type' => 'post' or 'capbility_type' => 'page' which will give anyone whoever has access to posts or pages respectively the same access to your post type.


A few things to note:

1) Ensure your post type slug is the same. It may be labelled "Resume" but the slug may be different. Without seeing the post type registration code it's difficult to say.

2) You need to assign the query to a variable then loop that variable. This is a secondary query which is different the a normal query. You need to loop it yourself. The documentation has some good examples about a standard loop:

https://developer.wordpress/reference/classes/wp_query/#standard-loop

<?php 
    // the query
    $the_query = new WP_Query(array(
        'post_type' => 'resume',
        'author' => 1,
    ) );
?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- Reset the main query data -->
    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

3) Make sure that this is the author you're looking for. Usually ID 1 is the first author made for the site, like an Administrator.

发布评论

评论列表(0)

  1. 暂无评论