• Resolved giboo

    (@giboo)


    Hello
    since I struggled to make it works, I share my solution :
    I found the add_filter method and the generated query “join” fine but didn’t “order”.
    It’s just a matter of priority of the posts_orderby filter that wasn’t high enough.
    I’m using the List Category Post Plugin to get posts from a specific category I intended to order manualy thanks to this plugin.
    Here is my working code :

    global $wp_query, $wpdb;
    function filter_join($join){
      global $wp_query, $wpdb;
      $join .= " LEFT JOIN ".$wpdb->prefix."reorder_post_rel ON (".$wpdb->prefix."posts.ID = ".$wpdb->prefix."reorder_post_rel.post_id AND ".$wpdb->prefix."term_relationships.term_taxonomy_id = ".$wpdb->prefix."reorder_post_rel.category_id )";
        return $join;
    }
    function edit_posts_orderby($orderby){
      global $wp_query, $wpdb;
      $orderby = " ".$wpdb->prefix."reorder_post_rel.id ";
      return $orderby;
    }
    add_filter('posts_join', 'filter_join');
    add_filter('posts_orderby', 'edit_posts_orderby',1000);
    $wp_query->query;
    remove_filter('posts_join', 'filter_join');
    remove_filter('posts_orderby', 'edit_posts_orderby');
Viewing 1 replies (of 1 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    Nice one, thanks for the heads up on this. Given the huge number of installs of the plugin ‘List Category Post Plugin’, I am thinking this could make for a nice plugin extension.

    It isn’t fully clear why you needed to override the posts_join, isn’t an inner join sufficient on post IDs given that they are unique across all categories ?

Viewing 1 replies (of 1 total)
  • The topic ‘Reorder post within categories with $wp_query in other plugin’ is closed to new replies.