Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post type on the archive category page WordPress

  • SOLVED

Hi All,

I hope you can help me on below:

I have made a custom post type ('classifieds') and a corresponding
hierarchical taxonomy ('region'). Also, i want use existing taxonomy
'category' with my new CPT (custom post type).

All works fine, except i can't create archive category page for my
CPT. I am using following query in category.php template:

<?php global $query_string; ?> //to get category ID where user is
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
//for pagination

<?php $my_query = new
WP_Query($query_string.'post_type=classifieds&post_status=publish&posts_per_page=8&paged='.$paged);
?> //my custom query
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<article>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php the_content(); ?>
</article>

<?php endwhile;
wp_reset_query();
?>

URL of my archive category page is as follows:
http://site_url/category/parent-category-name
or
http://site_url/category/parent-category-name/child-category-name

...however it doesn't work!
Ultimately, I need archive category page and page pagination that works.

UPDATED:
I found a mistake im my own code. In this line:

WP_Query($query_string.'post_type=classifieds&post_status=publish&posts_per_page=8&paged='.$paged);
shoud be additional symbol '&':

WP_Query($query_string.'&post_type=classifieds&post_status=publish&posts_per_page=8&paged='.$paged);

So, I have archive category page that shows CPT depending in what category (taxonomy 'category') user is. Now I only need working pagination on folowing page! I try use code from article by Kriesi (http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin).

Many thanks for you help on this one,

best regards,
Vitaliy

Answers (2)

2010-10-07

Oleg Butuzov answers:

not possible without custom parsing of the query...
you can use custom reqrites or query_parse for this...


Vitalij comments:

Can you write specific code for my example?


Oleg Butuzov comments:

add_filter('parse_query', 'parse_query_callback');

function parse_query_callback($wp_query){
/* here is your parsign stuff*/
return $wp_query;
}


this is simplest example... inside callback you need to parse arguments. sorry but there a lot work with debuging...

live example of such staff (just to inform whats actually can be inside)

add_filter('parse_query', 'parse_query_callback');

function parse_query_callback($wp_query){
if (strpos($url, get_option('home').'/event') !== false){

//$HLKEY = 'highlight@event';
//$exclude = array_filter(array_map('intval', (array) get_option($HLKEY)), 'intval');

if ($wp_query->query_vars['name'] == 'page'){
$wp_query->query_vars['paged'] = (int) substr($q->query_vars['page'],1);
unset($wp_query->query_vars['name']);
unset($wp_query->query_vars['event']);
unset($wp_query->query_vars['page']);
$wp_query->is_single = false;
$wp_query->is_singular = false;

//$q->query_vars['post__not_in'] = $exclude;


// ubrat iz lister'ahiglight
}

$wp_query->is_page = false;
$wp_query->query_vars['post_type'] = 'event';

if ($wp_query->query_vars['pagename'] != ''){
$wp_query->is_single = false;
$wp_query->is_singular = false;
$wp_query->query_vars['pagename'] = '';

//ubrat is listera higilight
//$q->query_vars['post__not_in'] = $exclude;

}

// custom filter 4 events
add_filter('option_posts_per_page', array(__CLASS__, 'option_posts_per_page_4_events'));

// templates filter
add_filter('index_template', array(__CLASS__, 'post_type_index_template'));
add_filter('page_template', array(__CLASS__, 'post_type_index_template'));
add_filter('single_template', array(__CLASS__, 'post_type_index_template'));

// posts where filter
add_filter('posts_where', array(__CLASS__, 'posts_where'));

}
return $wp_query;
}



and don't make custom queries inside template - course you will get 404 at the next page... base queries should be work inside query_parse.



Oleg Butuzov comments:

last example not relative to your structure.
however you can use preg_match for checking structure.... and set values in parse_query
(categoryname1|categoryname2|categoryname3)/(.*?)
its a just a quick example...

2010-10-07

Maor Barazany answers:

There is a lack in WP3.0.1 that can't create archive pages to CPT.
They should probably add this to WP3.1 or later version.

Meanwhile, you have to use [[LINK href="http://wordpress.org/extend/plugins/simple-custom-post-type-archives/"]]this plugin[[/LINK]].

After installing it, you should copy your archive.php (or index.php or category.php, what you need) and rename it to - <strong>type-slug,php</strong>, where slug is the slug you gave your CPT when registering it, in the line, i.e:

'rewrite' => array('slug' => 'news'),

in this example, the file would be <strong>type-news.php</strong>.

Then, browsing to www.yourdomain/news will bring you the archive of this CPT.

Hopes it will solve your issue.


Vitalij comments:

Thanks! But no, its doesn't work.


Maor Barazany comments:

1. Have you installed the plugin and activated it?
2. What is the exact code you register your post type with?
3. What is the file name you copied and duplicated?
4. What name you gave your new template name?
5. What permalinks structure do you use?
6. Can you see the archive page with <strong>www.domain.com?post_type=classifieds</strong> ?


Maor Barazany comments:

Also, in the type-{slug}.php, you created should find of course the start of the loop :

if (have_posts()) :
while (have_posts()) : the_post();


and add before it the query_posts function:

query_posts('posts_per_page=16&paged='.$paged.'&post_type=classifieds');


Maor Barazany comments:

And last think - Go to your permalink admin page and re-save it to flush the structure.


Maor Barazany comments:

Well it appears that this plugin works only for non-hierarcical CPT, so if it's not working well, it might be the issue....


Vitalij comments:

1.Yes
2.

$labels = array(
'name' => ('Classifieds'),
'singular_name' => ('Classifieds'),
'add_new' => ('Add new'),
'add_new_item' => ('Add new classifieds'),
'edit_item' => ('Edit classifieds'),
'new_item' => ('New classifieds'),
'view_item' => ('View classifieds'),
'search_items' => ('Search classifieds'),
'not_found' => ('Nothing found'),
'not_found_in_trash' => ('Nothing in trash'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'query_var' => 'classifieds',
'rewrite' => array('slug' => 'classifieds', 'with_front' => false),
'capability_type' => 'post',
'hierarchical' => false,
'taxonomies' => array( 'region', 'category'),
'menu_position' => 5,
'supports' => array('title','editor','author','thumbnail','excerpt','custom-fields','comments')
);
register_post_type('classifieds',$args);


3-4. I copied 'category.php' file and named it 'type-classifieds.php'
5. Pretty permalink '/%postname%/'
6. No

In my template files ('category.php' and now in 'type-classifieds.php') i use code, that i posted in first message.

I re-save permalink structure after 1-4.

I think you little misunderstand me. I can output whole my CPT on one page. But i want to create flexible query that shows CPT depending in what category (taxonomy 'category') user is.


Maor Barazany comments:

Well ok I played a bit with your code and found that in order to use the pre-defined taxonomies to work (i.e. category and post_tag), you should register them within a function and call it in the init action.

Something like this:




function register_my_classfields() {

$labels = array(
'name' => ('Classifieds'),
'singular_name' => ('Classifieds'),
'add_new' => ('Add new'),
'add_new_item' => ('Add new classifieds'),
'edit_item' => ('Edit classifieds'),
'new_item' => ('New classifieds'),
'view_item' => ('View classifieds'),
'search_items' => ('Search classifieds'),
'not_found' => ('Nothing found'),
'not_found_in_trash' => ('Nothing in trash'),
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'query_var' => 'classifieds',
'rewrite' => array('slug' => 'classifieds', 'with_front' => false),
'capability_type' => 'post',
'hierarchical' => false,
'taxonomies' => array( 'region', 'category'),
'menu_position' => 5,
'supports' => array('title','editor','author','thumbnail','excerpt','custom-fields','comments')
);

register_post_type('classifieds',$args);

} //here you close the function

add_action('init', 'register_my_classfields');




With that, I could assign regular categories to the new post type, since before it wasn't possible.
Also, I can browse to 'region' archive page, using <strong>domain.com/region</strong>
But, if you use the <strong>pre_defined</strong> category taxonomy, it won't show you the CPT that assigned to it, just the posts assigned to it.

In order to see the archives of a CPT that are categorized in the WP pre_defined category taxonomy, I could reach it via url:

<strong>http://localhost/tests/category/general/?post_type=classifieds</strong>

Where "general" is the category and then it shows me only items in 'general' category that are in CPT classfields

With the plugin I suggested, and I could all classfields in the url:

<strong>http://localhost/tests/classifieds</strong>

but something like -
<strong>http://localhost/tests/classifieds/general</strong>
wouldn't work.


If you will use a new hierarchical taxonomy (let's say calls it mycat) for your classfields CPT, that is not sharing other post type, you will be able to browse to it using
<strong>domain.com/mycat</strong> to see all it's archive.


Vitalij comments:

Thanks.
But, please, see 'updated' section of my question.


Maor Barazany comments:

What is the issue with pagination?
Isn't the default pagination works for you?
Try this first to see if you get regular pagination before you try Kriesi's code.


<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( '<span class="meta-nav">&larr;</span> Older posts'); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts <span class="meta-nav">&rarr;</span>'); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>



Anyway, you could always use the [[LINK href="http://wordpress.org/extend/plugins/wp-pagenavi/"]]wp-pagenavi[[/LINK]] plugin to get pagination, it works well also ith CPT.


Maor Barazany comments:

Of course, replace the <strong>$wp_query</strong> in your custom query variable, I see you named it <strong>$my_query</strong>


Vitalij comments:

My custom query looks like:
$my_query = new WP_Query($query_string.'&post_type=classifieds&post_status=publish&posts_per_page=2&paged='.$paged);
and it works fine.

Both regular pagination and wp-pagenavi doesn't work. Both even doesn't show number of pages at bottom.

Kriesi's code shows number of pages at bottom, but links for any of this pages doesn't work (error 404).