WordPress.org

Ready to get started?Download WordPress

Forums

ReOrder Post Within Categories
reorder-post-within-categories with wp_query (6 posts)

  1. laetitia Godet
    Member
    Posted 11 months ago #

    How can we make a wp_query orderby in a post with reorder Within categories ??

    https://wordpress.org/plugins/reorder-post-within-categories/

  2. laetitia Godet
    Member
    Posted 10 months ago #

    How to use reorder Within cartegories in a wp_query orderby of??
    Thank you

  3. Lulu_n10
    Member
    Posted 6 months ago #

    Hi Laetitia,

    It is not possible with the plugin to add an order by in the wp_query.

    The solution is to add filter on the query.

    Example :

    function filter_join($join){
    	global $wp_query, $wpdb;
    	$join .= " LEFT JOIN ".$wpdb->prefix."reorder_post_rel ON cg_posts.ID = ".$wpdb->prefix."reorder_post_rel.post_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');
    $query = new WP_Query(array(
    	'post_type' => 'your_post_type',
    	...
    ));
    remove_filter('posts_join', 'filter_join');
    remove_filter('posts_orderby', 'edit_posts_orderby');
  4. GeroDolja
    Member
    Posted 4 months ago #

    Does this work? I need a solution too. I have a modular front-page with different queries of different categories (like 3 posts on a featured category).

    I need a way to order that featured category in the homepage with this plugin.

    Thanks.

  5. laetitia Godet
    Member
    Posted 4 months ago #

    IT WORK

  6. T-Mike
    Member
    Posted 4 months ago #

    Lulu_n10's answer did not work for me when querying posts inside one taxonomy term ; mainly because I had several posts within multiple taxonomy terms.

    Just updated the LEFT JOIN from above answer and now it seems ok.

    Here's the full code :

    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');
    $query = new WP_Query(array(
    	'post_type' => 'your_post_type',
    	...
    ));
    remove_filter('posts_join', 'filter_join');
    remove_filter('posts_orderby', 'edit_posts_orderby');

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.