0

Firstly; I have searched high and low for a solution to the problem I am having (over the last 2-3 weeks), but nothing I have done has been able to get the pagination working for a post displaying a grid of posts. I would include more links in this post (including website in question) but I am limited to posting 2 urls.

(The theme being used is Divi, by Elegant Themes. ..child theme is being used)

So:
I have created a CPT and am using Advanced Custom Fields (ACF) to enable a simple admin page for the user to enter details (for each new post). This creates the new post of the CPT.

Posts that are created with this CPT are automatically listed on a 'Travel Blog' page in grid format. (..removed site URL..)

When a post from the Travel Blog page is selected for viewing, it displays an introductory page [post] that displays a grid of all blog posts with the category that is relevant for that page (trip) - not the CPT. It is this page where I would like the pagination, and these are the pages where the problem is.

The code for creating the CPT and for displaying the grid of posts is the following: http://pastebin.com/mM3XPUHM

This page (amongst many many others I have read) would appear the best resource I can find, but maybe there is something I haven't understood? How to fix pagination for custom loops?

Maybe some useful info
- the page displaying the post grid to be paginated is a Post as opposed to a page (with reference to WP terminology)

  • the grid of posts being displayed (where pagination is not working) are not of the CPT. They are displayed as per the relevant category - an ACF variable

  • when mouse pointer hovers over 'Older Posts' link, url is shown with '/page/2/' added, but when clicked, the first initial page is shown

  • if tested, the variable of 'max_num_pages' will return a number greater than 1, so there are numerous pages to display

  • I am not able to manually enter the URL with /page/2/ added, in the address bar. Initial page is displayed.

  • if I enter a fixed category for the post grid (as opposed to the ACF variable) it makes no difference, except for displaying a grid of posts of the same category on each page

  • if I enter a number for 'paged' in code. eg. '$args['paged'] = 3' the third page will actually be displayed

-- with the previously mentioned test, the link for 'Newer Posts' is still not displayed. Only 'Older Posts' is displayed.

I realise this is probably a reasonably long post and maybe there are still important details I have not mentioned, but here's hoping that someone might have some suggestions.

Thanks

Progress Edit:

While reading the WP codex for pagination it suggested setting permalinks to default, so I set to plain (I did have 'Post Name' selected) and the pagination actually works. I would much prefer using 'Post name' for permalinks, but hopefully this should help with fault finding.

2 Answers 2

0

I'm guessing you are confusing the query with your pagination logic. You are saying if get_query_var('paged') is false get get_query_var('page') but you are NOT on a static front page and then else kicks in and sets $args['paged'] = 1 so essentially always giving you the first set of posts in the loop.

I would try writing the query this way instead.

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // If we have a 'paged' parameter, set $paged to that value, if not set it to 1

$args = array(
   'post_type' => 'post',
   'posts_per_page' => 6,
   'paged' => $paged, // Our current paged parameter set above.
   'order' => 'desc',
   'orderby' => 'date',
   'tax_query' => array(
       array(
           'taxonomy' => 'category',
           'field'    => 'slug',
           'terms'    => get_field('category_to_display'), // category from the Advanced Custom Field variable
       ),
   ),
);


$the_query = new WP_Query( $args );

also, I noticed you are setting next post to older post

next_posts_link( '« Older Posts' );

and previous post to newer post

previous_posts_link( 'Newer Posts »' );

wouldn't you want that the other way around? But then it might just be me who's not thinking straight on that one!

26
  • Thank you for having a look. Your suggestion is what I had originally (which didn't, and still doesn't make any difference sadly - I did just try). The code I posted here was an effort to try something else. ...the reason for 'next' and 'previous' links being the way they are, is because only one of them is displayed (previous_posts_link doesn't get displayed). I figured that, as list is 'desc', the next posts to display are older posts. Again, just an attempt at trying various options. Unfortunately for me, I have gotten myself in a little over my head, but am determined to work this out.
    – HeyBlondie
    Commented Aug 5, 2016 at 11:36
  • Ok, a few more question are coming up in my mind. Where is the custom query code residing (in archive-major_trip.php, in functions.php ...)? Also, are you using the default WP htaccess rules or do you have some customization/more rules there? The query looks good like it is, so the issue is coming from elsewhere.
    – bynicolas
    Commented Aug 5, 2016 at 16:59
  • Yes, I'm sure there is something I have done, or have overlooked. I have made 'single-major_trip.php' which is in the child directory. The standard 'single.php' file for the theme was used as a base, but I am starting to wonder if that's where the problem lies? (it still has some references that are specific to the Divi theme, although I have removed many already). I am looking now at creating a new single.php file with only what is required as bare minimum for WP. As for htaccess, I have made no changes - only plugins such as WordFence maybe?
    – HeyBlondie
    Commented Aug 6, 2016 at 4:05
  • ...have been reading about wp_link_pages(). Would this need to be included at all? I haven't seen it referenced before in other articles/information for pagination.
    – HeyBlondie
    Commented Aug 6, 2016 at 4:33
  • Well maybe that's your problem. single.php (or single-CPT.php) are for, well single post page. You are working with an archive of posts for the grid and with a single when you click on one post. I would like to see your single-major_trip.php if you could pastebin it. As for wp_link_pages, you don't need that for what you are trying to accomplish
    – bynicolas
    Commented Aug 7, 2016 at 4:16
0

This issue has finally been resolved for me. In short, the solution is posted here: https://wordpress.org/support/topic/url-rewriting-on-custom-post-type/#post-7720726

...and this is the eventual code that was added to the functions.php file in the child theme:

function fix_request_redirect( $request ) {
    if ( isset( $request->query_vars['post_type'] )
         && 'custom_type' === $request->query_vars['post_type']
         && true === $request->is_singular
         && - 1 == $request->current_post
         && true === $request->is_paged
    ) {
            add_filter( 'redirect_canonical', '__return_false' );
    }

    return $request;
}
add_action( 'parse_query', 'fix_request_redirect' );

...'custom_type' is to be replace by the actual title of the Custom Post Type.

Thank you for your effort at helping @bynicolas

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.