1

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?

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.org/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.

  • I edited the question with the register post type code – www.matemingler.com Jul 31 '19 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.