Some time ago, I submitted a question that was successfully answered by Rempty here:
https://wpquestions.com/Rerwrite_permalink_based_on_ACF_radio_button_selection/19615
I still rely on this code today.
At the time, there wasn't enough content so I didn't worry about pagination. But now that there is, I am getting a 404 on pages beyond the first page on my custom post type archive page for this this post type. I have a feeling some additional conditions need to be met specifically regarding the rewrite rules.
I thought I could find a solution in this post, but it never got me there:
http://meigwilym.com/fixing-the-wordpress-pagination-404-error/
I am using archive-news.php and not a page template. Here is the full post type registration:
function news_post_type() {
// all the labels stuff is here but i removed it for this post
);
$args = array(
'labels' => $labels,
'show_in_menu' => true,
'description' => false,
'public' => true,
'capability_type' => 'post',
'menu_icon' => 'dashicons-media-text',
'menu_position' => 0.0,
'page-attributes' => true,
'supports' => array( 'title','thumbnail','excerpt','revisions','comments'),
'has_archive' => 'news',
'query_var' => true,
'rewrite' => array('slug' => 'news/%news_type%')
);
register_post_type( 'news', $args );
}
add_action( 'init', 'news_post_type' );
function rem_news_rewrite( $post_link, $id = 0 ){
$post = get_post($id);
if (is_object($post) ) {
$ptype = get_post_type($post);
//Check the correct post type
if($ptype == "news") {
$news_type = get_field('field_58619925792a1',$post->ID);
if( $news_type ) {
return str_replace( '%news_type%' , $news_type , $post_link );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'rem_news_rewrite', 1, 3 );
add_filter('rewrite_rules_array', 'rem_news_rewrite_rules');
function rem_news_rewrite_rules($rules) {
$newRules = array();
$newRules['news/(.+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
return array_merge($newRules, $rules);
}
I can also provide a "Debug This" rewrite output if that's helpful.
Hai Bui answers:
try this, it would add an exception for the pagination urls:
add_filter('rewrite_rules_array', 'rem_news_rewrite_rules');
function rem_news_rewrite_rules($rules) {
$newRules = array();
$newRules['news/.+((?!page).+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
return array_merge($newRules, $rules);
}
David Holtz comments:
No change, even after saving permalinks.
That is if you suggestion was for me to replace the following:
___________________
add_filter('rewrite_rules_array', 'rem_news_rewrite_rules');
function rem_news_rewrite_rules($rules) {
$newRules = array();
$newRules['news/(.+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
return array_merge($newRules, $rules);
___________________
with this:
___________________
add_filter('rewrite_rules_array', 'rem_news_rewrite_rules');
function rem_news_rewrite_rules($rules) {
$newRules = array();
$newRules['news/.+((?!page).+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
return array_merge($newRules, $rules);
}
Hai Bui comments:
I'm sorry there was a mistake.
This should work:
add_filter('rewrite_rules_array', 'rem_news_rewrite_rules');
function rem_news_rewrite_rules($rules) {
$newRules = array();
$newRules['news/((?!page/).+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
return array_merge($newRules, $rules);
}
David Holtz comments:
This did it! Thank you so much!
Arnav Joy answers:
Can you show me your site please ?
David Holtz comments:
https://thebadcopy.com/news/
Arnav Joy comments:
can you show me code how you have displayed these posts ?
Liam Bailey answers:
You need to replace
$newRules = array();
$newRules['news/(.+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
With
$newRules = array();
$newRules['news/(.+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
$newRules['news/(.+)/(.+?)/page/([0-9]+)\/?$'] = 'index.php?news=$matches[2]&paged=$matches[3]';
If you want to use feeds etc you will need additional rules for that as well - let me know
David Holtz comments:
Thank you for your answer. Unfortunately, I still received a 404.