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

wp_get_archives for custom post types WordPress

I have a custom post type called video-blog.

I'm trying to create dated archives for just this post-type.
ex.
Archives
July 2013
June 2013
May 2013

This dated list needs to be able to live in a sidebar.

I have tried variations of wp_get_archives, but this either creates a 404 page. Or just displays posts from the main blog.

Answers (5)

2013-09-14

Luis Abarca answers:

You need to modify WordPress rewrite rules to do that.

Take a look on this example.

[[LINK href="https://github.com/Seebz/Snippets/blob/master/Wordpress/functions/register_post_type_with_dates.php"]]https://github.com/Seebz/Snippets/blob/master/Wordpress/functions/register_post_type_with_dates.php[[/LINK]]

2013-09-14

Arnav Joy answers:

You can write following in archive.php file

<? php query_posts ('post_type= video-blog');?>


Arnav Joy comments:

try this code in functions.php

<?php

add_filter( 'getarchives_where' , 'ucc_getarchives_where_filter' , 10 , 2 );

function ucc_getarchives_where_filter( $where , $r ) {

$args = array( 'public' => true , '_builtin' => false );
$output = 'names';
$operator = 'and';

$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) );
$post_types = "'" . implode( "' , '" , $post_types ) . "'";

return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where );
}
?>

2013-09-14

Liam Bailey answers:

Here is what I would do where I want to output the archive links - in sidebar or whatever:

<?php

$archive_args = array('echo' => false);
$dom = new DOMDocument()
$archives = wp_get_archive($archive_args);
$dom->loadHTML($archives);
foreach($dom->getElementsByTagName('a') as $link) {

$href = $link->getAttribute('href');
$paraset = (strstr($href,"?")) ? ":" : "?";
$newhref = $href . $paraset . "post_type=video-blog";
$link->setAttribute('href',$newhref);
}
echo $dom->saveHTML();


And then this in functions.php

add_action('pre_get_posts','show_post_type_archives');

function show_post_type_archives($query) {
if ($query->is_archive() && (is_date() || $query->is_date() && $_GET['post_type'] == "video-blog") {
$query->set('post_type','video-blog');
}

}

2013-09-15

Daniel Yoen answers:

Hello,

wp_get_archives doesn't work with custom post types, you should modify your query ike this :

in functions.php

add_filter( "getarchives_where","node_custom_post_type_archive",10,2);
function node_custom_post_type_archive($where, $args)
{
$post_type = isset($args["post_type"]) ? $args["post_type"] : "post";
$where = "WHERE post_type = "$post_type" AND post_status = "publish"";

return $where;
}


Then, your query looks like this :

<?php wp_get_archives(array("post_type" => "video-blog", "type" => "monthly")); ?>

Hope this help :-)


ktp12345 comments:

$where = "WHERE post_type = "$post_type" AND post_status = "publish""; This line is causing a php error.

2013-09-16

Remy answers:

Do you want to limit date archives only for this CPT, or create a specific dated archive for it and leave the other (for posts) available ?


ktp12345 comments:

I want to create a specific dated archive for this CPT and leave the other archive for posts.