Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Rewrite API, Current Month & Paging WordPress

  • SOLVED

I created a little function that let's me go to www.domain.com/current-month and it will display posts for the current month. www.domain.com/previous-month does essentially the same thing. Here's the code:

//add rules
add_action( 'init', 'bcg_rewrite_add_rules' );
function bcg_rewrite_add_rules() {
add_rewrite_rule( 'current-month/?([^/]*)',
'index.php?m=' . date("Y") . date("m"), 'top' );
add_rewrite_rule( 'previous-month/?([^/]*)',
'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ), 'top' );
}


The only problem is that I can't page through the posts. The Twenty Ten theme will set the Older Posts link to http://www.domain.com/current-month/page/2/ but browsing that page always takes me back to page 1 of the series of posts.

I'd like help figuring how to make the paging work. If the function for the rules above had to change, I'm open to that.

Thanks!
Kevin

Answers (3)

2011-09-13

Luis Abarca answers:

Give it try


//add rules

add_action( 'init', 'bcg_rewrite_add_rules' );

function bcg_rewrite_add_rules() {

add_rewrite_rule( 'current-month/?([^/]*)',

'index.php?m=' . date("Y") . date("m"), 'top' );

<strong>
add_rewrite_rule( 'current-month/page/([0-9]{1,})/?$',

'index.php?paged=$matches[1]&m=' . date("Y") . date("m"), 'top' );
</strong>

add_rewrite_rule( 'previous-month/?([^/]*)',

'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ), 'top' );

<strong>
add_rewrite_rule( 'previous-month/page/([0-9]{1,})/?$',

'index.php?paged=$matches[1]&m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ), 'top' );</strong>

}


I change the regexp, edit and then go to Settings > Permalinks and refresh the page

Im using it in this site http://michilpancingo.com/eventos/2011


Kevin McGillivray comments:

No dice. It didn't solve the problem. I still just saw the first page of records over and over when clicking on the link to see page 2. Any other ideas?


Luis Abarca comments:

I better share my rewrite function of my plugin


function events_add_rewrite_rules()
{
// 2001 events paged: /events/2011/page/x
add_rewrite_rule("eventos/([0-9]{4})/page/([0-9]{1,})/?$", 'index.php?post_type=jab_event&year=$matches[1]&paged=$matches[2]', 'top');

// monthly events paged: events/2011/09/page/x
add_rewrite_rule("eventos/([0-9]{4})/([0-9]{1,2})/page/([0-9]{1,})/?$", 'index.php?post_type=jab_event&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]', 'top');

// monthly events: events/2011/09
add_rewrite_rule("eventos/([0-9]{4})/([0-9]{1,2})/?$", 'index.php?post_type=jab_event&year=$matches[1]&monthnum=$matches[2]', 'top');

// all events paged: events/page/2
add_rewrite_rule("eventos/page/([0-9]{1,})/?$", 'index.php?post_type=jab_event&paged=$matches[1]', 'top');

// 2011 events: /events/2011
add_rewrite_rule("eventos/([0-9]{4})/?$", 'index.php?post_type=jab_event&year=$matches[1]', 'top');

}


URL:
[[LINK href="http://michilpancingo.com/eventos/2011/"]][[/LINK]]


Luis Abarca comments:

It actually works on this site

http://michilpancingo.com/eventos/2011/

[[LINK href="http://michilpancingo.com/eventos/2011/"]]http://michilpancingo.com/eventos/2011/[[/LINK]]


Kevin McGillivray comments:

Luis, can you tell me what your permalinks are set to? Thanks!


Luis Abarca comments:

yep !

The plugin call flush_rewrite_rules() on activation:

function register_jab_event_post_type()
{
$labels = array(
'name' => 'Eventos',
'singular_name' => 'Evento',
'menu_name' => 'Eventos'
);

$args = array(
'labels' => $labels,
'public' => true,
'rewrite' => array('slug' => 'eventos'),
'capability_type' => array('evento', 'eventos'),
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments', 'post-formats'),
'has_archive' => true,
'query_var' => 'eventos'
);

register_post_type('jab_event', $args);

events_add_rewrite_rules();
}

register_activation_hook(__FILE__, 'event_post_type_activation');

function event_post_type_activation()
{
register_jab_event_post_type();

flush_rewrite_rules();
}


Kevin McGillivray comments:

Hey, Luis. We're getting close! What I meant was on Settings > Permalinks, which option did you choose but it may not matter. I've been able to get this to work for URLs like www.domain.com/current-month/page/2


add_rewrite_rule( "current-month/page/([0-9]{1,})/?$", 'index.php?m=' . date("Y") . date("m") . '&paged=$matches[1]', 'top');
add_rewrite_rule( "previous-month/page/([0-9]{1,})/?$", 'index.php?m=' . date("Y") . date("m") . '&paged=$matches[1]', 'top');


But I can't get a rewrite rule for www.domain.com/current-month/ to work at the same time. Do you know what regular express I would use to determine when www.domain.com/current-month or www.domain.com/current-month/ were entered but without anything following that?

Thanks!
kcm


Luis Abarca comments:

It seems that you are using the same month for both rules.


add_rewrite_rule( "current-month/page/([0-9]{1,})/?$", 'index.php?m=' . date("Y") . date("m") . '&paged=$matches[1]', 'top');

<strong>
add_rewrite_rule( "previous-month/page/([0-9]{1,})/?$", 'index.php?m=' . date("Y") . date("m", strtotime("-1 month") ) . '&paged=$matches[1]', 'top');
</strong>


Luis Abarca comments:

upps! a little error


add_rewrite_rule( "current-month/page/([0-9]{1,})/?$", 'index.php?year=' . date("Y") . '&monthnum=' . date("n") . '&paged=$matches[1]', 'top');

add_rewrite_rule( "previous-month/page/([0-9]{1,})/?$", 'index.php?year=' . date("Y") . '&monthnum=' . date("n", strtotime("-1 month") ) . '&paged=$matches[1]', 'top');


Kevin McGillivray comments:

Hi, Luis. Okay, I've gotten it all worked out. Thanks for your help. Your rules above were part of the solution. The other issue was that I had to get "www.domain.com/current-month" to work without any paging parameters. Here's the final solution I came up with:


add_action( 'init', 'bcg_rewrite_add_rules' );
function bcg_rewrite_add_rules() {
// rewrite "current-month" to the current month of content. Allow paging.
add_rewrite_rule( "current-month/page/([0-9]{1,})/?$", 'index.php?m=' . date("Y") . date("m") . '&paged=$matches[1]', 'top');
add_rewrite_rule( "current-month/?$", 'index.php?m=' . date("Y") . date("m"), 'top');

//rewrite "previous month" to the previous month of content. Allow paging.
add_rewrite_rule( "previous-month/page/([0-9]{1,})/?$", 'index.php?m=' . date("Y", strtotime("-1 month") ) . '&paged=$matches[1]', 'top');
add_rewrite_rule( "previous-month/?$", 'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ), 'top');
}


Thanks!

2011-09-13

ej_emman answers:

ok just try this.


<?php
//Im thinking the reason why your add rules doesnt work because it always fits the first condition WHERE m='yourcurrentDate'
//I try to place GET method to filter this.
//add rules

add_action( 'init', 'bcg_rewrite_add_rules' );

function bcg_rewrite_add_rules($_GET) {

if(!isset($_GET['paged']))
{
add_rewrite_rule( 'current-month/?([^/]*)',

'index.php?m=' . date("Y") . date("m"), 'top' );

add_rewrite_rule( 'previous-month/?([^/]*)',

'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ), 'top' );

}
else
{
add_rewrite_rule( 'current-month/([^/]*)/?',

'index.php?m=' . date("Y") . date("m").'paged='.$matches[1], 'top' );

add_rewrite_rule( 'previous-month/([^/]*)/?',

'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ).'paged='.$matches[1], 'top' );
}

}
?>


Kevin McGillivray comments:

Hey, EJ. Isn't this just an alternative way of doing exactly what I'm already doing? It doesn't seem to address the paged records at all. Am I missing something?

Thanks!
Kevin


ej_emman comments:

I have update my previous ans. try it. I have explanation to it also. why it happens


ej_emman comments:

tell me it it helps.


Kevin McGillivray comments:

Nope. Same results: clicking older posts just shows the posts on page 1 even though the URL is "/current-month/page/2/"


ej_emman comments:

//Can you try this.. I forgot to place & to the previous code.

<?php
add_action( 'init', 'bcg_rewrite_add_rules' );

function bcg_rewrite_add_rules($_GET) {

if(!isset($_GET['paged']))
{
add_rewrite_rule( 'current-month/?([^/]*)',

'index.php?m=' . date("Y") . date("m"), 'top' );

add_rewrite_rule( 'previous-month/?([^/]*)',

'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ), 'top' );

}
else
{
add_rewrite_rule( 'current-month/([^/]*)/?',

'index.php?m=' . date("Y") . date("m").'&paged='.$matches[1], 'top' );

add_rewrite_rule( 'previous-month/([^/]*)/?',

'index.php?m=' . date("Y", strtotime("-1 month") ) . date("m", strtotime("-1 month") ).'&paged='.$matches[1], 'top' );
}

}
?>


ej_emman comments:

Heres my brief explaination. Hope this help
Ahm, actually your code is right. Where you redirect to current-month/ page when you encounter
index.php?m=' . date("Y") . date("m")... but the problem is you always redirect to this. when you use your pagination and add some parameter.

ex. index.php?m=201109 redirect to current-month/ is correct //this is w/o pagination
index.php?m=201109&paged=2 this will still redirect to current-month/ since it fits your m condition //with pagination

thats why I place $_GET to the condition. I know there is still another method to refilter this..


Kevin McGillivray comments:

I get what your saying about how the rule matches everything but your latest function didn't produce any different results. Clicking Older Posts still just shows page 1 of the results set.

2011-09-13

John Cotton answers:

Try this:


function bcg_rewrite_add_rules( $rules ) {
$newrules = array();

$newrules['^current-month/page/(\d*)?([^/]*'] = 'index.php?m=' . date("Ym").'&page=$matches[1]';
$newrules['^previous-month/page/(\d*)?([^/]*'] = 'index.php?m=' . date("Ym", strtotime("-1 month")).'&page=$matches[1]';
$newrules['^current-month?([^/]*'] = 'index.php?m=' . date("Ym");
$newrules['^previous-month?([^/]*'] = 'index.php?m=' . date("Ym", strtotime("-1 month"));

return $newrules + $rules;
}
add_filter('rewrite_rules_array','bcg_rewrite_add_rules');


You'll need to hit save on the permalinks page to activate it.


Kevin McGillivray comments:

There might be something wrong with your regular expression. I get this error when i load your function:


Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 33 in /home/XXXXXXX/wp-includes/class-wp.php on line 198

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 33 in /home/XXXXXXX/wp-includes/class-wp.php on line 199

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 34 in /home/XXXXXXX/wp-includes/class-wp.php on line 198

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 34 in /home/XXXXXXX/wp-includes/class-wp.php on line 199

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 22 in /home/XXXXXXX/wp-includes/class-wp.php on line 198

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 22 in /home/XXXXXXX/wp-includes/class-wp.php on line 199

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 23 in /home/XXXXXXX/wp-includes/class-wp.php on line 198

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 23 in /home/XXXXXXX/wp-includes/class-wp.php on line 199


John Cotton comments:

Do you know there is! Serves me right for cut n pasting...




function bcg_rewrite_add_rules( $rules ) {

$newrules = array();



$newrules['^current-month/page/(\d*)?([^/]*)'] = 'index.php?m=' . date("Ym").'&page=$matches[1]';

$newrules['^previous-month/page/(\d*)?([^/]*)'] = 'index.php?m=' . date("Ym", strtotime("-1 month")).'&page=$matches[1]';

$newrules['^current-month?([^/]*)'] = 'index.php?m=' . date("Ym");

$newrules['^previous-month?([^/]*)'] = 'index.php?m=' . date("Ym", strtotime("-1 month"));



return $newrules + $rules;

}

add_filter('rewrite_rules_array','bcg_rewrite_add_rules');


Kevin McGillivray comments:

Thanks. So this will only work if the permalinks are set to "Default" and if I change to "Default" the page no longer displays the correct title which is "Monthly Archives: September 2011" on any other permalink setting. If I set the permalinks to any other format, the Older Posts link just takes me back to the same (first) page of posts.

Any ideas?


John Cotton comments:

It shouldn't make a difference what permalink setting you have. All that does is tell WordPress how to interpret urls. In this case, you're doing the interpreting for it (note that your rules go to the top of the pile by being on the left of the return assignment.

I'm not sure I quite follow the rest of what you're saying. Can you put up a link?