Custom related posts output
-
Hello,
I’m a WordPress developer. I’m looking for a filter in this plugin that will allow me to specify exactly the ID of the posts that I want the plugin to return as related posts. Could you tell me if there is a filter like this and a brief code example? Thank you very much.
-
Curious to know how you’re looking to set this. You have a way to set this for each post in the meta box of each post.
The other option is to filter crp_query_args_before to update $args[‘include_post_ids’].
add_filter( 'crp_query_args_before', 'update_args_for_crp', 10, 2); function update_args_for_crp( $args, $source_post ) { $args['include_post_ids'] = '5,10'; return $args; }
I would like to use your plugin so that instead of displaying related articles, the user can define which posts should be promoted on their website as “related articles”. Although it is possible to do this individually for each post, I will create an administrative panel so that it is possible to apply this configuration to all posts in a specific category, for example.
Unfortunately this filter didn’t work. I even inserted a wp_die to test if the script is executed but nothing happens.
I also tested it by defining in a post’s meta box which related articles I want to be shown, but although it works, it’s not exactly what I need as other posts are still displayed, even if they’re not in this metabox.
How can I programmatically set the ID of posts that should exclusively appear as related articles of a specific post in your plugin?
Am I reading this correctly that you want to only display the related articles and nothing else? i.e. if you have the related articles set e.g. in the meta field then you don’t have anything else displayed?
I’m thinking there might be a better way to do this than using the query args etc (I need to check why that didn’t work). The include_posts only adds these to the rest, it doesn’t force the specific posts. But, if you’re building out a panel, another thing worth exploring is using CRP_Query and setting the posts using post__in of WP_Query as it is a wrapper.
Am I reading this correctly that you want to only display the related articles and nothing else? i.e. if you have the related articles set e.g. in the meta field then you don’t have anything else displayed?
Yes, you’re right.
I’m thinking there might be a better way to do this than using the query args etc (I need to check why that didn’t work). The include_posts only adds these to the rest, it doesn’t force the specific posts. But, if you’re building out a panel, another thing worth exploring is using CRP_Query and setting the posts using post__in of WP_Query as it is a wrapper.
Could you give me an example code, like you did with the filter above? I can’t resolve this, I’m sorry.
I was thinking and I’m not 100% sure if it works, but what happens if you try this code? Using post__in with the set of posts you want to include.
add_filter( 'crp_query_args_before', 'update_args_for_crp', 10, 2); function update_args_for_crp( $args, $source_post ) { $args['post__in'] = '5,10'; return $args; }
Unfortunately it didn’t work. I can provide you with the site’s data over a secure channel, if you are interested in investigating. Thank you for your help.
I had to bit a digging as I realised I had quite more going on with that variable. Here’s code that works. Note that this might only work with v3.5.0 which I’m releasing later today.
function crp_overwrite_posts_pre_query( $posts, $query ) { $post_ids = array( '12923' ); if ( ! empty( $post_ids ) ) { $posts = get_posts( array( 'post__in' => array_unique( $post_ids ), 'fields' => $query->get( 'fields' ), 'orderby' => 'post__in', 'numberposts' => $query->get( 'posts_per_page' ), 'post_type' => $query->get( 'post_type' ), ) ); $query->found_posts = count( $posts ); $query->max_num_pages = (int) ceil( $query->found_posts / $query->get( 'posts_per_page' ) ); } return $posts; } add_filter( 'crp_query_posts_pre_query', 'crp_overwrite_posts_pre_query', 20, 2 );
Obviously, worth adding that you’ll need to write code to change the $post_ids to what you want exactly. This one forces all posts to have a post with ID 12923.
For some reason, 2 posts are always returned, even though I only specified 1 post id.
I did the following test:
Instead ofreturn $posts;
I just did:return [];
And still 1 post continues to be returned.It’s almost done. Only need to figure out how to fix this issue. Thank you very much.
-
This reply was modified 5 months, 3 weeks ago by
remiCs.
Which post is returned and what is the full code that you are using?
I’m wondering if that is coming from somewhere else and would need to be tracked down. Can you check that the cache is disabled until everything is finalised. Then can you install Query Monitor plugin and see what queries are generated on the page/post you’re viewing. The main one is the one with the MATCH clause in there, but also need to see what other queries related to fetching posts are being generated by the plugin.
Thanks. I’m wondering if this is a sticky post. Do you know what exactly this random post is, especially when you’re forcing a blank. Can you try passing this to the get_posts to ignore sticky posts.
ignore_sticky_posts = true
I reached the post settings, and it is not a sitcky post.
Furthermore, messing with the get_posts function in the case of this specific error will have no effect because I am not returning the $posts variable but rather an empty array for testing purposes.Good point. I’m at a loss as I can’t replicate it at my end. The only thing I can suggest is disabling other plugins and/or switching the theme.
Were you able to track down that particular post and what exactly is it? One option is can you try turning it to a draft or private temporarily.
Jesus Christ! I previously used metabox to manually set which posts should be displayed on a specific post for testing purposes and forgot to remove this setting. Your code works. Your plugin is 5 stars ?, thank you very much!
I woke up this morning thinking the very same thing and was going to suggest this!
Please do consider leaving a review.
https://www.ads-software.com/support/plugin/contextual-related-posts/reviews/
-
This reply was modified 5 months, 3 weeks ago by
- You must be logged in to reply to this topic.