WordPress.org

Forums

Sort carousel using $args['orderby'] (1 post)

  1. Dani1091
    Member
    Posted 1 year ago #

    Hello friends,

    I need your help with this: I want to sort my carousel of popular coupons as you can see here (the second one) sorting it from comment_count to gdsr_sort with thumbs (using old plugin GD Star rating).

    Hoping someone can guide me. I'm a beginner on php. Regards.

    Here's the code who calls the popular coupons carousel to the homepage:

    <div class="row collapse">
            <div class="column">
                <div class="items-carousel flexslider">
                    <ul class="rr slides">
                        <?php
                        $args = array();
                        $args['posts_per_page'] = 20;
                        $args['post_type'] = 'coupons';
                        $args['orderby'] = 'comment_count';
                        $args['order'] = 'DESC';
                        $query = new WP_Query($args);
                        while($query->have_posts() ) : $query->the_post();
                            $discount = get_post_meta($post->ID, '_single_discount', true);
                            $date = get_post_meta($post->ID, '_single_date', true);
                            $code = get_post_meta($post->ID, '_single_code', true);
                            $url = get_post_meta($post->ID, '_single_url', true);
                            $thumbs = array();
                            $categories = get_the_category();
                            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                            if($categories) {
                                foreach($categories as $category) {
                                    $image = get_option('taxonomy_' . $category->term_id);
                                    if($image) {
                                         if($thumb == '') {
                                            $thumb = $image['custom_term_meta'];
                                        }
                                        $category_id = $category->term_id;
                                    }
                                }
                            }
                            $daysleft = round( ($date-time()) / 24 / 60 / 60);
                            ?>

    And here is the code of functions.php who defines the 'gdsr_sort' sorting of posts.

    //modify-ing the custom query on category pages
    
    add_action( 'parse_query','changept' );
    function changept() {
    	if( (is_category() || is_author() ) && !is_admin() ) {
    		set_query_var( 'post_type', array( 'post', 'coupons' ) );
    	}
    	if( (is_category() || is_search() || is_author() || is_tag() ) && !is_admin() ) {
    		if(isset($_GET['sort']) && $_GET['sort'] == 'popular') {
              	set_query_var('orderby', 'comment_count');
              	set_query_var('order', 'desc');
            }
            elseif(isset($_GET['sort']) && $_GET['sort'] == 'thumbs') {
                set_query_var('meta_query', array(
                    array(
                         	'key' => '_single_date',
                         	'value' =>  strtotime("now"),
                         	'compare' => '>=',
                         	'type' => 'UNSIGNED'
                     	)
                ) );
                set_query_var('gdsr_sort', 'thumbs');
                set_query_var('sort_order', 'desc');
            }

Topic Closed

This topic has been closed to new replies.

About this Topic