Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am developing a blog where the main posts are of a custom post type.

Let's assume it is a Book custom post type.

I want the /author/admin page to list the books of the author admin, but instead, it goes and searches for Posts of admin, which is the built-in basic post type.

How do I make my custom post type Book to be used in all such index queries? (main page, author, search, etc).

share|improve this question
add comment

3 Answers 3

You can modify all main queries before they happen via the pre_get_posts action and a check if is_main_query:

function wpa75492_post_type_query( $query ) {
    if ( $query->is_main_query() ) {
        $query->set( 'post_type', array( 'book' ) );
    }
}
add_action( 'pre_get_posts', 'wpa75492_post_type_query' );
share|improve this answer
add comment

use the parameter post_type=your-custom-post-type-slug-here

for example if your post type slug is "product" then use the code like:

query_post("post_type=product");
share|improve this answer
    
Additionally if you want to keep default post types too use: [code] global $wp_query; $args = array_merge( $wp_query->query_vars, array( 'post_type' => 'product' ) ); query_posts( $args ); [/code] more details: codex.wordpress.org/Function_Reference/query_posts –  Shaon Dec 9 '12 at 18:40
1  
I strongly advise against the use of query_post, it causes more problems than its worth, Milo's answer demonstrates the correct alternative to query_post –  Tom J Nowell Dec 9 '12 at 19:09
add comment

You need to query the posts from that specific post type before the loop. So, on your author archive page, or any page template, add this before the loop:

query_posts(array(
    'post_type'   => 'book_posttype',
    'author_name' => 'mark'
));

Query_posts can take other parameters as well.

http://codex.wordpress.org/Function_Reference/query_posts

It can use the same parameters as WP_query. One of which is author_name.

http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

You can also add an author page template which you will place this query in and the loop. It would be a page in your theme directory called, author-mark.php or whatever the name is.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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