WordPress.org

Ready to get started?Download WordPress

Forums

Archive Widget for Custom Post Type (3 posts)

  1. TSardi
    Member
    Posted 4 months ago #

    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: tomi.com/product/2014/03/12
    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:
    tomi.com/2014/03/?post_type=product
    tomi.com/?m=201403&post_type=product

    2. How to generate a URL like tomi.com/product/2014/03 using wp_get_archives()?

    I used following code to generate tomi.com/?post_type=product&m=201403

    $ 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.

  2. bcworkz
    Member
    Posted 4 months ago #

    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.

  3. TSardi
    Member
    Posted 4 months ago #

    Great, thanks!

    I've done with rewrite rules. Using this link as an advice:
    http://wordpress.org/support/topic/writing-wp_rewrite-gtnon_wp_rules-to-htaccess?replies=13

    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.

Reply

You must log in to post.

About this Topic