admin管理员组文章数量:1418709
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 badges1 Answer
Reset to default 1Whenever 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.
本文标签: wp querynew WPQuery with author argument return no results
版权声明:本文标题:wp query - new WP_Query with author argument return no results 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745281004a2651423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论