• Resolved ririro

    (@ririro)


    I’m using the plugin to load custom related posts into my existing theme layout. The setup is pretty simple:

    $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

    (@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!

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

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