skip to Main Content

We have a page on one of our websites which outputs a WP Query loop of data with a custom post type we have setup within ACF called "Client Stories". In our WP Query we’re also using the page navigation numbers so you can go to page 2, 3 etc.

Everything works perfectly, the post type works and the page links to the next/prev pages too.

However, one thing we’d like to change is the URL structure which we can see under Advanced Settings > URLs. We’d like to change it from the default post type key which is "client-story" to a custom permalink called "client-stories".

When we enter this custom permalink URL the WP Query still works absolutely fine and bring through the data and the URL has successfully changed, however the page numbers navigation stops working and we get a 404 – Page Not Found error when trying to click on page 2 etc.

We’d like the URL to be /client-stories/story-1/ instead of /client-story/story-1/ but unsure how to do it without breaking the page number navigation.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(  
    'post_type' => 'client-story',
    'post_status' => 'publish',
    'posts_per_page' => 9,
    'paged' => $paged,
);
 
$loop = new WP_Query($args);
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
 
    $current_page = max(1, get_query_var('paged'));
 
    echo '<div class="pagination_wrapper">';
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('Prev'),
            'next_text'    => __('Next'),
        ));
    echo '</div>';
}

2

Answers


    1. Goto the Settings -> Permalinks and hit save
    2. Open the base url on front-end
    3. Clear the Cache
    4. Check the custom post type page

    It should be helpful.

    Login or Signup to reply.
    1. Update Custom Post Type Permalink: Make sure the custom post type registration includes the new rewrite slug
      'rewrite' => ['slug' => 'client-stories'],
    1. Flush rewrite rules: This can be done by going to settings > Permalinks
    2. Adjust Pagination Base: The issue with the paginate_links function calls to use a custom base that aligns with the update URL structure
    echo paginate_links(array(
        'base' => home_url('/client-stories/page/%#%'),
        'format' => '?paged=%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('Prev'),
        'next_text'    => __('Next'),
    ));
    1. Testing: After making these changes test your website specifically the URLs
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search