Support » Plugins » Hacks » Archive Widget for Custom Post Type

  • I need to create Plugin for Custom Post Type (Product) and got problems when creating widgets Archive for the CPT.

    1. I got 404 for this URL:
    I suspect this problem rewrite rule. I’ve read some articles but still no clue (or maybe I missed some important links out there?)

    The following URLs are worked:

    2. How to generate a URL like using wp_get_archives()?

    I used following code to generate

    $ options = wp_get_archives ($ parameters);
    $ options = str_replace ('? m =', '? post_type=product&m=', $ options);

    But the code was only valid if I use the Default Permalink.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    Date permalinks don’t work with CPTs by default, you need to add a rewrite rule to change the URL you want to one that works.

    That still will not fix wp_get_archives(). For that you need to alter the query used to identify the archive dates, then also alter the link used to get the archives. This can be done with filters, but you will need to use conditionals that determine which page the archive is on in order to know if the filters should be applied or not.

    Another alternative is to create a CPT archive widget that gets loaded in CPT sidebars. If you don’t like that option, the query filter is ‘getarchives_where’. You need to change ‘post’ in the WHERE string to your CPT slug. The use ‘get_archives_link’ filter to inject the CPT slug into the URL in a way that is recognized by your rewrite rule.

    Great, thanks!

    I’ve done with rewrite rules. Using this link as an advice:

    And now facing wp_get_archives(). I used getarchives_where filter.

    function widget()
        add_filter('getarchives_where', array($this, 'getarchives_where_filter'), 10, 2);
        $parameters = array(
            'format'          => $format,
            'show_post_count' => $count,
            'echo'            => false
        $options = wp_get_archives($parameters);
        $options = str_replace('?m=', '?post_type=product&m=', $options);
        remove_filter('getarchives_where', array($this, 'getarchives_where_filter'), 10);
    function getarchives_where_filter($where)
        $where = str_replace("post_type = 'post'", "post_type = 'product'", $where);
        return $where;

    The problem is how to adjust the URL to custom permalink. Since the code above worked only for default permalink.

    Right now, I’m going to check your alternative solution.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Archive Widget for Custom Post Type’ is closed to new replies.