Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter laetitia Godet

    (@laetitia-godet)

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

    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');

    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.

    Thread Starter laetitia Godet

    (@laetitia-godet)

    IT WORK

    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');

    Hi, can I know how does the above code work? Does it work in such a way that I can display the first 3 post of a categories that is arranged by the plugin?

    Cos Im stuck trying to achieve the above result.

    Thread Starter laetitia Godet

    (@laetitia-godet)

    Yes you can do it simply change the wp_query like this :
    $query = new WP_Query(array(
    ‘post_type’ => ‘your_post_type’,
    ‘showposts’ => 3,
    ‘category__in’ => array(idCat)
    ));

    Hi laetitia Godet, thanks for your reply.

    so i need to add the below into my theme’s function.php right? But how do I output the result onto a page template? Via shortcode?

    Really appricate your help.

    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');
    Thread Starter laetitia Godet

    (@laetitia-godet)

    you can add in your template or fonction.php

    Hi laetitia Godet, i have added to the following into a custom page template.

    Not sure if its correct, cos the output is still the same
    https://sghome2buy.com/test

    <div class="span9 main-wrap">
    
                    <!-- Main Content -->
                    <div class="main">
    
                        <section class="listing-layout <?php if( $view_type == 'grid' ){ echo 'property-grid'; } ?>">
    
                            <div class="view-type clearfix">
                                <?php
                                $page_url = custom_taxonomy_page_url();
                                $separator = (parse_url($page_url, PHP_URL_QUERY) == NULL) ? '?' : '&';
                                ?>
                                <a class="list <?php if( $view_type != 'grid' ){ echo 'active'; } ?>" href="<?php echo $page_url.$separator.'view=list'; ?>"></a>
                                <a class="grid <?php if( $view_type == 'grid' ){ echo 'active'; } ?>" href="<?php echo $page_url.$separator.'view=grid'; ?>"></a>
                            </div>
    
                            <div class="list-container clearfix">
                                <?php
    
    							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(
    							'property-status' => 'landed-for-sale',
    							'post_type' => 'property',
    							'showposts' => 3,
    						));
    						remove_filter('posts_join', 'filter_join');
    						remove_filter('posts_orderby', 'edit_posts_orderby');
    
                                if ( $query->have_posts() ) :
                                    while ( $query->have_posts()) : $query->the_post(); 
    
                                        if( $view_type == 'grid' ){
                                            /* Display Property for Grid */
                                            get_template_part('template-parts/property-for-grid');
                                        }else{
                                            /* Display Property for Listing */
                                            get_template_part('template-parts/property-for-listing');
                                        }
    
                                    endwhile;
                                else:
                                    ?>
                                    <div class="alert-wrapper">
                                        <h4><?php _e('No Results Found', 'framework') ?></h4>
                                    </div>
                                    <?php
                                endif;
                                ?>
                            </div>
    
                            <?php theme_pagination( $wp_query->max_num_pages); ?>
    
                        </section>
    
                    </div><!-- End Main Content -->
    
                </div> <!-- End span9 -->
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘reorder-post-within-categories with wp_query’ is closed to new replies.