get_related only next and previous post
-
Using the code provided in the Wiki for related posts is great. But it displays all the posts that are connected to the same parent. How can I make it so only the “next” and the “previous” post show. The next and previous post being determined by their creation dates preferably.
Any idea?
-
After a bit of searching I tried the following code that was posted here before..
<?php // Collect the relevant ids $episode_id = get_the_ID(); $prev = p2p_type( 'posts_to_anime' )->get_previous( $episode_id, $anime_id ); $next = p2p_type( 'posts_to_anime' )->get_next( $anime_id, $episode_id ); // Display what we found if ( $prev ) echo get_permalink( $prev ); if ( $next ) echo get_permalink( $next ); ?>
This code is in the single-episode.php page. I grab the anime ID, which the current episode is connected to through a while loop I do in the beginning. I’ve echoed both anime_id and episode_id at this exact point to see if they were correct and yes they are. But nothing is being outputted. I removed the if’s to see what is being generated, and it simply generates the current post’s permalink.
If you could help at all, it’ll be great.
Do I have to have sortable set to something when making my connection? Currently I don’t have it set to anything. But the problem is that when I do set it to something it screws my post order over.
Any sort of help would be appreciated on this subject.
The reason get_permalink() doesn’t work is because
get_previous()
andget_next()
don’t returnWP_Post
objects; they returnP2P_Item_Post
objects.This is fixed in the development version (1.5.2-alpha2).
Installed the developer version and tried the following code with no success.
This code was used for testing purposes just to see if it’ll go through the if statements, and it didn’t. Meaning the $prev and $next variables aren’t returning the array or don’t have any info in them.
I also tried a combination of $episode_id and $anime_id using them interchangeably to see if it had any effect.
<?php // Collect the relevant ids $episode_id = get_the_ID(); $prev = p2p_type( 'posts_to_anime' )->get_previous( $episode_id, $anime_id ); $next = p2p_type( 'posts_to_anime' )->get_next( $anime_id, $episode_id ); // Display what we found if ( $prev ) { echo 'prev'; get_permalink( $prev ); the_permalink( $prev ); } if ( $next ) { echo 'next'; get_permalink( $next ); the_permalink( $next ); } ?>
However, when I look at your recent update here: https://github.com/scribu/wp-posts-to-posts/blob/master/tests/test-core.php#L442-464
It uses the get_prev function instead of get_previous. But in a wiki page you use get_previous, so I’m a little lost as to what I should be testing with now.
Edit: Changing it to get_prev didn’t help either.
I actually did have echo in front of get_permalink functions.
Try using get_adjacent_items() then:
https://github.com/scribu/wp-posts-to-posts/blob/master/tests/test-core.php#L448-489
I’m not an expert in PHP, in fact probably not intermediate either. From the code provided, I didn’t understand what the variable “$this” referred to, and i’d assume it’s referring to the object that is currently being worked with but I have no clue.
I tried the following though
$prev = p2p_type( 'posts_to_anime' )->get_adjacent_items( $episode_id );
The variable $episode_id is basically the ID of the current post that the user is on, so it’ll be
get_the_ID()
. This statement just gets the exact same ID and in the permalink prints out the permalink of the post that you are currently on instead of the permalink of the previous or the next post.Actually, get_adjacent_items() returns 3 post objects:
$items = p2p_type( 'posts_to_anime' )->get_adjacent_items( $episode_id ); echo get_permalink( $items['parent'] ); if ( $items['previous'] ) echo 'previous: ' . get_permalink( $items['previous'] ); if ( $items['next'] ) echo 'next: ' . get_permalink( $items['next'] );
Also, are you sure your connection type has
'sortable' => true
?I had a feeling it’ll work when Sortable was set to true and it did.
But having it set to sortable causes much bigger problems for me. I don’t want my users to be able to screw around with the order of the posts. With sortable set to false or not have sortable defined in registering the connection they used to automatically order themselves in an ASC order by post date.
Now with sortable set to true the next and previous links work (although weirdly). Furthermore, it automatically puts my list of connected posts in DESC order by post date. And the query orderby value / order value has no effect on it for some reason.
Additionally the previous and next post links are displaying each other’s value. I guess that could be because of the way the connection order is working out and so I guess this problem would likely be solved when I can have the connection order sorted.
Would it be possible to have sortable set to false and still have previous and next functions work?
or possibly make it so you can set a default order and not let users actually change the order of the connections.
At this point in time, I know that’s asking for too much. So how would I go about actually sorting the posts by their post dates in an ascending order instead of descending order as it is now, while having the previous and next post links work.
Sorry for causing so much trouble.
Additional information. Seems like I have to manually go to all the parent posts and manually update them for the previous and next post links to show up or they don’t actually show up.
Is this because I’ve recently changed the connection order to sortable and will be needing to do this every time I make a change of that sort?
After further testing I’ve noticed that setting sortable to ‘any’ simply screws with the way the connected posts appear in the admin panel. The posts are completely jumbled up and can only be sorted manually using the icons on each post, which is probably what was intended. However, I have over 500 posts connected to one post, and they were originally sorted by their dates in an ascending order. Now that they are jumbled up, the next and previous links act extremely weird.
Furthermore, I was able to get the WP_Query to order the connected posts correctly without much problem. Just because of the sorting done in the admin panel for connected posts is affecting the next and previous links quite extremely. I can’t have them manually sorted either because of the sheer number of posts connected. There are over 200 instances like this.
Having the next and previous work with sortable set to default (false) would probably be the best solution.
Now I understand what you’re getting at. You can’t get adjacent items based on the post date, using the current API, but it would be a nice feature to have:
Could I possibly edit the function to reflect this change? Would it be a simple edit or multiple things need to be changed for this to work?
In fact, it would be even better to define “connection_order” and “connection_orderby” right when you are registering your posts 2 posts connection. If you set sortable to true or something, then this still remains as the default order and now you have the choice to manually sort it if you’d like?
That way the get_adjacent_posts should work even when sortable is set to false or true
The problem is that WP_Query doesn’t allow you to say “give me the post(s) that have post_date greater or smaller than this post here”.
You’d have to use the ‘posts_where’ filter to create a custom query var that does that.
It’s not rocket science, but it’s not trivial either.
Makes sense, it’ll definitely be better if posts-2-posts supported this functionality natively.
Being able to do something of this sort
p2p_register_connection_type( array( 'name' => 'posts_to_pages', 'from' => 'post', 'to' => 'page', 'connection-order' => 'post-date', 'connection-orderby' => 'DESC' ) );
^ That would really help and this way, we could probably have the connections sorted by basically anything, like custom post meta fields and such.
This way sortable can be set to false so users aren’t able to sort the posts and admins such as myself can still define a default order they’d like the connections to be in.
If sortable is set to true, then this still remains the default order, only that now users can sort the order if they’d like. Basically how it is now.
I think this maybe a more long term way to go. I have no clue how it’s handled though, so I guess i’ll leave it up to you.
But if there is a short term way of mass fixing the connection order to post date as ASC while making get_adjacent_items function working as sortable is set to false would be great.
- The topic ‘get_related only next and previous post’ is closed to new replies.