Title: Custom ordering
Last modified: September 19, 2023

---

# Custom ordering

 *  Resolved [ririro](https://wordpress.org/support/users/ririro/)
 * (@ririro)
 * [2 years, 8 months ago](https://wordpress.org/support/topic/custom-ordering-3/)
 * I’m using the plugin to load custom related posts into my existing theme layout.
   The setup is pretty simple:
 *     ```wp-block-code
       $custom_relations = CustomRelatedPosts::get()->relations_to($current_post_id);
   
       $custom_relation_ids = array_keys($custom_relations);
       ```
   
 * What I wanted to ask: is there any way to adjust this logic so it contains the
   ordering matches the custom ordering from the post page where the links are created?
 * Much appreciate the help and great plugin!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Brecht](https://wordpress.org/support/users/brechtvds/)
 * (@brechtvds)
 * [2 years, 8 months ago](https://wordpress.org/support/topic/custom-ordering-3/#post-17063550)
 * 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:
 *     ```wp-block-code
       usort( $custom_relations, function( $a, $b ) { return $a['order'] - $b['order']; } );
       ```
   
 *  Thread Starter [ririro](https://wordpress.org/support/users/ririro/)
 * (@ririro)
 * [2 years, 8 months ago](https://wordpress.org/support/topic/custom-ordering-3/#post-17063670)
 * 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:
 *     ```wp-block-code
       $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!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom ordering’ is closed to new replies.

 * ![](https://ps.w.org/custom-related-posts/assets/icon-256x256.png?rev=2007814)
 * [Custom Related Posts](https://wordpress.org/plugins/custom-related-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-related-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-related-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-related-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-related-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-related-posts/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [ririro](https://wordpress.org/support/users/ririro/)
 * Last activity: [2 years, 8 months ago](https://wordpress.org/support/topic/custom-ordering-3/#post-17063670)
 * Status: resolved