• Resolved dskbrk

    (@dskbrk)


    hello!

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

    site.com/category/catname/date/2010/04

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

    function extend_date_archives_flush_rewrite_rules(){
    	global $wp_rewrite;
    	$wp_rewrite->flush_rules();
    }
    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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Remove the top function and action, the code will then stop running on every page.

    Thread Starter dskbrk

    (@dskbrk)

    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?

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

    Thread Starter dskbrk

    (@dskbrk)

    Cool!

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

    Thank you so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Archives by both date and category’ is closed to new replies.