Pagination of custom post types in category and archives

465 views
Skip to first unread message

Justinas Bolys

unread,
Feb 2, 2012, 1:26:16 AM2/2/12
to magic-...@googlegroups.com
Hi there,

I'm using magic fields to create listings of the newest scene releases. I have created types as "movies", "games" with custom fields as "release date", "genre" and so on.

After installing magic fields and testing it I have noticed that my custom type posts does not appear on my index and category pages (for some strange reason it shows up on search).

So after googling for some time I have found some useful code which I have added to the category.php and index.php and archive.php of the template:

global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'any' ) );
query_posts( $args ); 

After that my custom type posts appears on my pages which is very good.

But then appeared another problem, which I was not able to fix myself.

It was pagination, which shows newer/older pages, BUT after I go to the second page I got 404 error. This happens only on category and archive pages.
But works fine on index page. Also works fine if I create category full of default type posts. 

Has anybody else met this kind of problem? 

BTW, I have tried Custom Post Type Category Pagination Fix which had to fix this, but it did not work for me.

I have even done some debugging by adding print_r($args) after the code which I have pasted above, and that did not appear when I go to the second category page, so that's not the template issue for sure.

Justinas Bolys

unread,
Feb 2, 2012, 5:38:41 AM2/2/12
to magic-...@googlegroups.com
Did some more testing.

If I add "&post_type=movie" to the paged URL and i get something like:
/?cat=4&paged=2&post_type=movie

then paging works fine without 404 error (shows default post type and movies too), but if I add "&post_type=any" it gives me 404 error. 

I think there is small glitch in wordpress system, that checks only default post type if post_type is not set in GET variables. I just need to find that place and set that to ANY by default.

I will give live example:
Working fine: http://scenedumps.org/?cat=4&post_type=movie&paged=2 (showing even default type posts because I have set to show ANY post type on the template with code above).

Justinas Bolys

unread,
Feb 2, 2012, 5:45:38 AM2/2/12
to magic-...@googlegroups.com
Okay, sorry for the flood, but I have found something that I needed.

in wp-includes/query.php I have changed:

if ( !isset($q['post_type']) ) {
if ( $this->is_search )
$q['post_type'] = 'any';
else
$q['post_type'] = '';
}

to 

if ( !isset($q['post_type']) ) {
if ( $this->is_search )
$q['post_type'] = 'any';
else
$q['post_type'] = 'any';
}
 

Now works fine I guess.  

Muffin

unread,
Feb 2, 2012, 6:21:12 AM2/2/12
to Magic Fields
Don't edit the wordpress files.

Use this instead and place it in your functions.php file in your
template folder (if you don't have one, create one)....

function include_custom_post_types( $query ) { global $wp_query;
if ( is_category() || is_tag() || is_search() || (is_archive())) {
$post_types = array('posttype', 'posttype');
$custom_post_type = get_query_var( 'post_type' );
if ( empty( $custom_post_type ) ) $query->set( 'post_type' ,
$post_types );
}
return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );



----------------------------------------------



and replace the 'posttype' from this line with => $post_types =
array('posttype', 'posttype'); <= with your post types.

Justinas Bolys

unread,
Feb 2, 2012, 6:34:12 AM2/2/12
to magic-...@googlegroups.com
Worked fine. Thanks a LOT. 

Such a changes MUST be added to the wiki. I have been trying to solve this for almost 24hours. ;)

Muffin

unread,
Feb 2, 2012, 6:36:12 AM2/2/12
to Magic Fields
No problem.

I'm going to create a website for such things.

I have a lot of fixes / add-ons for magic fields.

David.
Reply all
Reply to author
Forward
0 new messages