Sort by featured

Discontinued - The forum content is out of date. For theme support please use our support desk here.

Sort by featured

February 26, 2019
Alexey Alexey
  • Alexey
    Alexey
    Newbie 10 points

    Hello. Anybody know how i can sort coupons by featured in store page?

    February 26, 2019 at 4:21 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    Hello Alexey,
    Honestly, I haven’t checked specifically before updating to 9.2.5, but I do see it within the sort options in the last v9 theme update (9.2.5). Update the theme and see if it’s there.

    (Sorry for the original typo on the greeting)

    February 27, 2019 at 3:39 am
  • Alexey
    Alexey
    Newbie 10 points

    Hello james. Yes 9.2.5 have sort option in store page. But i need make featured sort by default.

    February 28, 2019 at 11:26 pm
  • Marie
    Marie
    Super Guru 1,747 points

    hi Alexey,

    you will have to mdify the file if you want to set Featured as default sort

    March 1, 2019 at 7:40 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    Hello Alexey,

    Add this to your ‘_functions.php’ file and see if that helps ;-).

    /**
     * Set to show 'Featured' first.
     *
     * @param WP_Query Object $wp_obj
     * @return WP_Query Object $wp_obj
     */
    function _custom_sort($wp_obj){
        $wp_obj->set('meta_key', 'featured');
        $wp_obj->set('orderby', 'DESC');
        return $wp_obj;
    }
    add_filter( 'pre_get_posts', '_custom_sort');
    March 1, 2019 at 9:19 am
  • Chris
    Chris
    Newbie 36 points

    @JamesD

    I have a similar question, actually opposite in a way. I want to pre-sort all my expired coupons to the bottom before display. I currently do this after page load with jQuery, however, I then get expired coupons on all 3 pages of results. It would make for better UX if I could presort this. Seems as if you are the only one capable of answering that here :)

    I tried your above code for “featured” and switched the meta_key to “expired” but it just removed all coupons.

    Any thoughts?

    March 30, 2019 at 1:20 pm
  • JamesD
    JamesD
    Super Guru 1,351 points

    Hello Chris,

    Does this do what you’re looking for?
    /**
    * Change coupon order by expiry date.
    *
    * ASC = Order with the ones to expire first (soon to expire)
    * DESC = Order with the last to be expired first
    *
    * @param WP_Query Object $wp_obj
    * @return WP_Query Object $wp_obj
    */
    function _custom_order($wp_obj){
    $wp_obj->set('meta_key', 'expiry_date');
    $wp_obj->set('orderby', 'meta_value');
    $wp_obj->set('order', 'ASC');
    }
    add_filter( 'pre_get_posts', '_custom_order');

    March 31, 2019 at 12:22 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    @Chris
    See if this helps with changing the number of coupons displayed. Replace NUMBER_OF_POSTS with the number you want to display.
    Add this line: $wp_obj->set('posts_per_page', NUMBER_OF_POSTS); directly under the $wp_obj->set('order', 'ASC'); line.

    March 31, 2019 at 12:51 am
  • Chris
    Chris
    Newbie 36 points

    @JamesD
    Thank you for the reply :)

    When I tried

    /**
    * Change coupon order by expiry date.
    *
    * ASC = Order with the ones to expire first (soon to expire)
    * DESC = Order with the last to be expired first
    *
    * @param WP_Query Object $wp_obj
    * @return WP_Query Object $wp_obj
    */
    function _custom_order($wp_obj){
    $wp_obj->set('meta_key', 'expiry_date');
    $wp_obj->set('orderby', 'meta_value');
    $wp_obj->set('order', 'DESC');
    }
    add_filter( 'pre_get_posts', '_custom_order');

    It did seem to mostly work, pushing my expired listings to bottom of sort. Except on a couple of tag pages. I’d have to change the way I use expiration dates currently.
    But the problem is it weirdly removed all front page listings and my main navigation menu.
    ᗧ···ᗣ···ᗣ··
    Any way to just target expired listings like is done to add “expired” class?

    <?php if($vv['expired'] == 1){ echo "expired"; } ?>

    I then tried the number of listings snippet, but isolated like:

    function _custom_order($wp_obj){
    $wp_obj->set('posts_per_page', 20);
    }
    add_filter( 'pre_get_posts', '_custom_order');

    And this worked fantastic, but I use the “Used Today” elementor widgets some sidebars. And this increased those posts from 5 to 20 also which tanked the page. Do you know how to isolate the per page to only main content listings?

    Thanks again!

    Thanks again!

    April 2, 2019 at 7:50 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    @Chris

    Any way to just target expired listings like is done to add “expired” class?

    You’ll need to loop through all the results in the page you want to add the classname (within what ever template file you’re referring to).

    Do you know how to isolate the per page to only main content listings?

    To change per-post on front page only:
    add_action( 'pre_get_posts', '_custom_front_perpage');
    function _custom_front_perpage($wp_obj){
    if( is_front_page() && !is_admin() && !$wp_obj->is_feed() && $wp_obj->is_main_query()) {
    $wp_obj->set('posts_per_page', -1);
    }
    }

    Note: -1 = all listings
    (I’ve added a couple more conditions above)
    Here’s a list of conditions that may help for various scenarios:
    https://codex.wordpress.org/Conditional_Tags

    April 3, 2019 at 10:53 pm
  • JamesD
    JamesD
    Super Guru 1,351 points

    @Chris
    Was wondering if you had the class childtheme_coupon_core class in your _functions.php or any other class in there? If so, then it may be better to put the above function within the class itself and call the ‘add_action’ from within the ‘__construct’ section.

    Here’s some code you can add (in conjunction with the above code) if you wanted to change the expiration meta query:

    	        $curr_meta_query = array( 'expirydate' => array(
    	            'key' => 'expiry_date',
    	            'value' => date('Y-m-d H:i:s',  time()),
    	            'compare' => '<=',
    	            'type' => 'DATETIME'
    	        ));
    	        $wp_obj->set( 'meta_query', $curr_meta_query);

    This example would look for all the coupons that have expired prior to that moment.
    Note: The setting PremiumPress > Configuration > Coupon Settings > SHOW EXPIRED COUPONS would need to be turned on.

    April 4, 2019 at 1:13 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    disregard

    April 4, 2019 at 5:53 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    @Chris
    The above meta query code should “target” only expired.

    And, the key to the per-page is to use the conditionals available. The weird things you’ve seen can be fixed using the proper conditional. You can pinpoint quite a bit with them.

    April 4, 2019 at 6:02 am
  • JamesD
    JamesD
    Super Guru 1,351 points

    Chris, please note that I’ve been assuming you’re somewhat familiar with PHP. Let me know if this isn’t the case and if anything should be elaborated on.

    April 4, 2019 at 6:30 am
Viewing 14 posts - 1 through 14 (of 14 total)

Didn't find what you were after?

Chat with us now

or contact our support team