自定义帖子类型的wp_get_archives不起作用?

时间:2012-01-20 14:52:51

标签: wordpress

我正在使用wp_get_archives检索已在我的自定义帖子类型“下载”中发布的帖子的每月日期列表。

但它不起作用,我做错了什么?

我正在使用workpress 3.3

由于

CODE

<?php   $args       = array(

    'post_type'     => 'download',
    'type'          => 'monthly',
    'show_count'    => '1'

); ?>

<?php wp_get_archives( $args ); ?>


这就是我如何注册我的自定义帖子类型以供参考

// Downloads Post Type
add_action( 'init', 'create_post_type' );
function create_post_type() {
    $args = array(
        'labels' => post_type_labels( 'Download' ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'has_archive' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array( 'group' ),
        'supports' => array('title',
            'editor',
            'author',
            'thumbnail',
            'excerpt',
            'comments'
        )
    ); 

    register_post_type( 'download', $args );

}

2 个答案:

答案 0 :(得分:1)

wp_get_archives();不支持'post_type'作为参数。

讨论[此处] [1]:http://wordpress.org/support/topic/archive-list-and-page-for-custom-post-types-mysql?replies=9关于黑客攻击支持它。本文提供了一些使用过滤器的解决方案的链接,该过滤器应用于sql查询以选择要包含在归档结果中的帖子。看一下如何在general-template.php中应用过滤器,它也应该有效。

答案 1 :(得分:0)

一些提示:

提示1 使用custom-post-type-archives插件(更改后更新永久链接)

提示2 WPML插件+自定义后期类型存档:

function CPT1_join( $join ) {
    global $wpdb;
    $join = " JOIN pav_icl_translations t ON t.element_id = pav_posts.ID AND t.element_type='CUSTOM_POST_TYPE1' ";
    return $join ;
}

function CPT2_join( $join ) {
    global $wpdb;
    return $join = " JOIN pav_icl_translations t ON t.element_id = pav_posts.ID AND t.element_type='CUSTOM_POST_TYPE2' "; 
}

function wp_get_archives_filter($where, $options) {
    global $wpdb; // get the DB engine

    if(!isset($options['post_type'])) return $where; // OK - this is regular wp_get_archives call - don't do anything

    $post_type = $wpdb->escape($options['post_type']); // escape the passed value to be SQL safe

   if($post_type == 'all')
           $post_type = ''; // if we want to have archives for all post types
    else $post_type = "post_type = '$post_type' AND"; // otherwise just for specific one

    if($options['post_type'] == 'CUSTOM_POST_TYPE1'){
        add_filter( 'getarchives_join', 'CPT1_join' );      
        }elseif($options['post_type'] == 'CUSTOM_POST_TYPE2'){ 
        add_filter( 'getarchives_join', 'CPT2_join' );
        }

   $where = str_replace('post_type = \'post\' AND', $post_type, $where);
   return $where;
}
add_filter('getarchives_where', 'wp_get_archives_filter', 10, 2);