Skip Navigation

[Resolved] Restrict post navigation on single template to category

This thread is resolved. Here is a description of the problem and solution.

Problem: On the single post template, I would like the next and previous links to point to the next or previous post in the same category. Several posts will have the same publish date and time.

Solution: Use a custom date field to store the post creation date and time whenever a post is created.

add_action( 'save_post', 'autoupdate_photo_sort', 10, 3 );
function autoupdate_photo_sort( $post_id, $post, $update ) {
  if ( $post->post_type =='photo' ) {
    if( empty( get_post_meta($post_id, 'wpcf-sorttime', true))){
      update_post_meta( $post_id, 'wpcf-sorttime', date('U'));
    }
  }
}

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by jillT 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#924143

I have a custom post type with a custom template for single page.
I have navigation on this:
[wpv-post-previous-link format="« %%LINK%%" link="Previous Photo"] [wpv-post-next-link format="%%LINK%% »" link="Next Photo"]

The problem is that this navigation takes it to the previous photo, regardless of category.
I need the navigation restricted to the same category, so previous goes to previous post in SAME category

#924318

Hi, there's not an easy way to do filter the next/previous post link based on a category like this. Can a photo have more than one category term associated with it? If so, then I'm not sure how that could work from a technical perspective, since the next and previous posts will be different for different categories. If not, then you might explore creating a View of these posts, filtered by category term set by the current post, and by post date supplied by shortcode attributes. Set the number of posts limit to 1, and insert a link to the post in the loop output editor. If this sounds like something you want to explore, let me know.

#924353

Hi Christian. Thank you very much for your reply.

The photo will indeed be only in ONE category.

Yes, your possible solution does sound as though it would be good, and I would be ever so grateful if you are able to help me on that!

#924415
Screen Shot 2018-07-12 at 5.38.20 PM.png

Okay sure, here are some pointers:
- Create a new View of these posts.
- Be sure "Don't include current page in query results" is checked.
- Order by post date, ascending
- Add a Query Filter that filters by the taxonomy term, set by the current page (see attachment).
- Add a post date filter that filters by year, month, day, hour, minute, and second, and finds posts after the date, using shortcode attributes (see attachment).
- Insert this View in the template for the single post, and fill in the shortcode attributes with information from the current post:

Next post: [wpv-view name="your-next-view-slug" year="[wpv-post-date format='Y']" month="[wpv-post-date format='n']" day="[wpv-post-date format='j']" hr="[wpv-post-date format='H']" min="[wpv-post-date format='i']"]<br />

- Once you have this working as expected, duplicate the View and change the Query Filter to find posts before the date. Everything else can stay the same in the filters.
- Insert this duplicate View in the template for the single post and use the same shortcode attributes as before:

Previous post: [wpv-view name="your-previous-view-slug" year="[wpv-post-date format='Y']" month="[wpv-post-date format='n']" day="[wpv-post-date format='j']" hr="[wpv-post-date format='H']" min="[wpv-post-date format='i']"]<br />

If you need more detailed instructions, I will return on Sunday.

#924861

Hi Christian. Hope you enjoyed your days off! Thank you so much for your help, which is really appreciated.

I got the method you gave working as it should, but another little problem hit me. A large number of the posts have exactly the same date and time, as they go into pending to be approved and they can all be approved at the same time! This of course confuses the order.

Is it at all possible to have it navigate using Post ID instead of Post Date? I have tried to look into that but it's beyond me 🙁

#947887

Okay I see what you mean. There's not a way to tell a Query Filter to find the next greater or next lower post ID combined with a taxonomy filter, so I don't think that's going to be possible, but we could do something similar with a custom field value. What if we set up a custom date field that is automatically defined with the current timestamp whenever one of these posts is created? Then use that field in the filter instead of the post date or post ID. Is the sorting order always based on the order in which the posts were created?

#948483

[Is the sorting order always based on the order in which the posts were created?]

Probably. Yes, I'm sure I can answer that with yes. Just want the latest posts to be shown first

That sounds a good possibility. I can create a date field, but I'm not sure how to populate it.

#948487

Thinking about this, the biggest problem I have at the moment are the posts already created which have the approved date and time, lots that are exactly the same. Out of 965 posts I do not know which ones will need changing!

I don't suppose that there is a way that I can deal with these without having to go through them and manually do the date?

#948674

To populate the date field when a post is first saved, you can use the save_post filter like this:

add_action( 'save_post', 'autoupdate_photo_sort', 10, 3 );
function autoupdate_photo_sort( $post_id, $post, $update ) {
  if ( $post->post_type =='photo' ) {
    if( empty( get_post_meta($post_id, 'wpcf-sorttime', true))){
      update_post_meta( $post_id, 'wpcf-sorttime', date('U'));
    }
  }
}

Modify the post type slug and the custom date/time field slugs as needed. After you add this code, you can use the batch update feature in wp-admin to update all the posts with this function. It's not completely automated, but it's better than editing each post manually. You can also write a custom script or SQL query - those fall outside the scope of support we provide here. Another option is to export the posts as CSV, add the correct timestamp custom field values in the CSV file, then re-import them.

https://toolset.com/documentation/user-guides/how-to-import-content-into-wordpress-using-csv/import-content-csv-importer-plugin/

#952076

Thank you!