0

I have inherited an existing site and have recently been asked to make some changes.

They have 'renamed' the default post as 'latest-news' using this 'my_new_default_post_type':

<?php

add_action( 'init', 'my_new_default_post_type', 1 );

    function my_new_default_post_type() {

      register_post_type( 'post', array(
          'labels' => array(
              'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
          ),
          'public'  => true,
          '_builtin' => false,
          '_edit_link' => 'post.php?post=%d',
          'capability_type' => 'post',
          'map_meta_cap' => true,
          'hierarchical' => false,
          'rewrite' => array('slug' => 'latest-news','with_front' => false),
          'query_var' => true,
          'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
      ) );

  }

?>

These can be seen being listed on a standard page here:

https://gateleyplc.com/news-events/latest-news/

with the second page of pagination here:

https://gateleyplc.com/news-events/latest-news/page/2/

Clicking into a post loads it here:

https://gateleyplc.com/latest-news/gateley-plc-celebrates-sixth-anniversary-in-yorkshire-with-the-appointment-of-six-new-solicitors/

I have added some new custom post types, one example of which is 'corparate-deals'. The code to add this custom post type is:

<?php

    add_action('init', 'corporate_deals_register');

    function corporate_deals_register() {

     $labels = array(
        'name' => _x('Corporate Deals', 'post type general name'),
        'singular_name' => _x('Corporate Deals', 'post type singular     name'),
        'add_new_item' => __('Add New Briefing'),
        'edit_item' => __('Edit briefing'),
        'new_item' => __('New briefing'),
        'view_item' => __('View briefing'),
        'search_items' => __('Search Corporate Deals'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
      'labels' => $labels,
      'public' => true,
      'has_archive' => false,
      'show_ui' => true,
      'capability_type' => 'post',
      'taxonomies'  => array( 'category' ),
      'hierarchical' => false,
      'publicly_queriable' => false,
      'show_ui' => true,
      'show_in_nav_menus'  => false,
      'exclude_from_search' => false,
      'menu_icon' => 'dashicons-welcome-add-page',  // Icon Path
      'menu_position' => 5,
      'show_in_menu'  =>    'blogs_menu',
      'query_var' => true,
      'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions' ),
    );

    register_post_type( 'corporate-deals' , $args );
  }

?>

You can see these listed here:

https://gateleyplc.com/corporate-deals

and clicking into one here:

https://gateleyplc.com/corporate-deals/gateley-corporate-team-advises-on-firms-acquisition-of-kiddy-partners/

However... when you try to view the second page of pagination, here:

https://gateleyplc.com/corporate-deals/page/2/

it redirects to:

https://gateleyplc.com

I have searched high and low for any redirects or rewrite rules but cannot find any.

One thing I did notice is that if I add:

'rewrite' => array('slug' => 'latest-news','with_front' => false),

to my custom post declaration (i.e. rewriting the 'corporate-deals' to 'latest-news') then the redirection doesn't occur.

Any help greatly appreciated. Thanks in advance.

Additionally

The code that builds the post listing is:

<?php // Corporate Deals

function paginatedcorporatedeals_func( $atts ) {

  extract( shortcode_atts( array(
    'pageid' => '{$pageid}',
    'title' => '{$title}',
    'showcontent' => '{$showcontent}',
    'newsurl' => '{$newsurl}',
    'postmeta' => '{$postmeta}'
  ), $atts ) );

  $output;

  global $post;

  $ws = 1;

  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  $args = array(
    'post_type'=>'corporate-deals',
    //'order'=>'ASC',
    'posts_per_page' => 16,
    'paged' => $paged,
  );

  $slug = the_slug();

  query_posts($args);

  // global $wp_query;

  // $postcount = $wp_query->found_posts;

  $count=0;

  if(have_posts()) {

    $divider = 'yes';
    $featuredimg = 'date';
    $truncate = 250;

    while (have_posts()) : the_post();

      global $post;

      $thetitle = get_the_title($post->ID);

      require get_template_directory() . '/assets/inc/plugins/vc-intergration/modules/shortcode-templates/media-list.php';

      $output .="<hr>";

      $count++;

    endwhile;

    $output .=  "<div class='vc_row clearfix'><div class='white-bar'>".page_pagination()."</div></div>";

    wp_reset_query();
  }

  return $output;

}

add_shortcode( 'paginatedcorporatedeals', 'paginatedcorporatedeals_func' );

?>
3
  • Is there a reason why you have created https://gateleyplc.com/corporate-deals as a page with a shortcode, rather than use the custom post type archive page that WordPress generates?
    – Milo
    Aug 14, 2018 at 15:43
  • I would rather have used an archive page as you suggest. I was trying to follow the existing layout/standards that the site has been built with. The page remains editable 'around' the list content and styling wise is in-keeping with the 'latest-news' list page. Restyling the auto generated archive pages to match the 'latest-news' page and list plugin will be my last resort. Thanks for the feedback. Aug 15, 2018 at 9:55
  • When you visit /corporate-deals/page/2/, WordPress is querying for the 2nd page of a singular corporate-deals post with the slug page. If you used the built-in archive instead of a page, this issue would disappear. You'll need to add your own rewrite rules to override the ones generated for your post type.
    – Milo
    Aug 15, 2018 at 15:38

3 Answers 3

0

just remove template redirect using remove template_redirect filter

use below code to stop the canonical redirect.

remove_filter('template_redirect','redirect_canonical');

2
  • I added that at the bottom of functions.php for the theme and unfortunately it is still redirecting. Aug 14, 2018 at 13:31
  • You should test for a specific condition in redirect_canonical and return false in that one case rather than completely remove the filter, which will break all kinds of other cases where you will want that redirect.
    – Milo
    Aug 15, 2018 at 12:59
0

According to me, the reason for these paging problems is 'publicly_queriable' => false, in your $args for register_post_type(). Change it to TRUE (default value) or comment out and pagination will work.

publicly_queriable argument should not be false unless the post type you are registering is not public.

1
  • I tried this and unfortunately it didn't work (although I think your suggestion is the right thing to do generally.) Thanks for the answer Aug 15, 2018 at 9:53
0

Changing the method may help. There needs to be a function.

<?php

function wpdocs_get_paginated_links( $query ) {
// When we're on page 1, 'paged' is 0, but we're counting from 1,
// so we're using max() to get 1 instead of 0
$currentPage = max( 1, get_query_var( 'paged', 1 ) );

// This creates an array with all available page numbers, if there
// is only *one* page, max_num_pages will return 0, so here we also
// use the max() function to make sure we'll always get 1
$pages = range( 1, max( 1, $query->max_num_pages ) );

// Now, map over $pages and return the page number, the url to that
// page and a boolean indicating whether that number is the current page
return array_map( function( $page ) use ( $currentPage ) {
return ( object ) array(
"isCurrent" => $page == $currentPage,
"page" => $page,
"url" => get_pagenum_link( $page )
);
}, $pages );
}

?>

And now you can use logic to display the pagination, don't forget is have to be outside the loop.

<!-- PAGINATION : begin -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args2s = array(
   'post_type' => 'portfolio_page',
   'posts_per_page' => 9,
   'paged' => $paged,

);
$categorie_portfolio = new WP_Query($args2s);
 if ($categorie_portfolio->max_num_pages > 1) : ?>
    <nav class="pagination">
      <h2 class="screen-reader-text">Pagination</h2>
    <ul class="pagination__list">

    <?php foreach( wpdocs_get_paginated_links( $categorie_portfolio ) as $link ) : ?>
    <?php if ( $link->isCurrent ): ?>
    <li class="pagination__item pagination__item--current"><a href="<?php esc_attr_e( $link->url ) ?>" class="pagination__item-link"><?php _e( $link->page ) ?></a></li>

    <?php else : ?>
    <li class="pagination__item" ><a href="<?php esc_attr_e( $link->url ) ?>" class="pagination__item-link"><?php _e( $link->page ) ?></a></li>
    <?php endif; ?>
     <?php endforeach; ?>
    </ul>
  </nav>

    <!-- PAGINATION : end -->
    <?php endif; ?>
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.