Ready to get started?Download WordPress


[resolved] Archives by both date and category (5 posts)

  1. dskbrk
    Posted 4 years ago #


    I'm trying to figure out how to have a working permalink structure like:


    I found this code snipplet here: http://snipplr.com/view.php?codeview&id=17432

    function extend_date_archives_flush_rewrite_rules(){
    	global $wp_rewrite;
    add_action('init', 'extend_date_archives_flush_rewrite_rules');
    function extend_date_archives_add_rewrite_rules($wp_rewrite){
    	$rules = array();
    	$structures = array(
    		$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
    		$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
    		$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
    	foreach( $structures as $s ){
    		$rules += $wp_rewrite->generate_rewrite_rules($s);
    	$wp_rewrite->rules = $rules + $wp_rewrite->rules;
    add_action('generate_rewrite_rules', 'extend_date_archives_add_rewrite_rules');

    and it does the job but is eating ~4 queries per page load, deleting and reinserting the rewrite_rules key in the options table on every page.

    There are more efficient ways to achieve this?

  2. Mark / t31os
    Posted 4 years ago #

    Remove the top function and action, the code will then stop running on every page.

  3. dskbrk
    Posted 4 years ago #

    oh, so I need to flush rewrite rules only if the permalink structure will change again, let's say, by admin option or another plugin?

  4. Mark / t31os
    Posted 4 years ago #

    Rules are generated when you save permalink settings, so you shouldn't even need the top function, i imagine it was put that way originally to avoid having to explain, that in order for the new rules to work - your permalinks would need to be saved again (since they're generated when you save them - ie. generate_rewrite_rules runs when you save permalink settings).

    I've never found saving the settings to be an issue and have always remembered to re-save them when adding custom rules, it makes no sense at all to flush them every page load, that's just silly (but an easy and lazy way to apply the rules without having to click Save on the permalink settings page).

  5. dskbrk
    Posted 4 years ago #


    Now I have a brighter vision, that function was really throwing me off the track.

    Thank you so much!

Topic Closed

This topic has been closed to new replies.

About this Topic