Custom ordering | WordPress.org

Plugin Author
Brecht

(@brechtvds)

Hi there,

You could check how it’s done in the /custom-related-posts/helpers/output.php file. It uses the sortByOrder function to sort the relations by their “order” key.

Something like this should work:

usort( $custom_relations, function( $a, $b ) { return $a['order'] - $b['order']; } );

Thread Starter
ririro

(@ririro)

Thanks a lot for your reply. That was helpful! Fixed it. In case it’s relevant for others or you find it fun to see:

$current_post_id = get_the_ID();
    
$custom_relations = CustomRelatedPosts::get()->relations_to($current_post_id);
    
$custom_relation_ids = array_keys($custom_relations);

    // Loop argumnetsnts show posts by category
    $args = array(
        'post__in' => $custom_relation_ids,
        'post__not_in' => array( $post->ID ),
        'orderby' => 'post__in' <-- added this

+ a bit lower in the code

  remove_all_filters('posts_orderby'); // ADDED

Got it to work. Again thanks!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer