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
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.
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!
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.
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 🙁
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?
[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.
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?
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/