Ask the community

This is a community support forum where you can ask
questions and interact with other PremiumPress Customers.

Sort by featured

  • Alexey
    Alexey
    Newbie 10 points
    February 26, 2019 at 4:21 am

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

  • JamesD
    JamesD
    Super Guru 1,345 points
    February 27, 2019 at 3:39 am

    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)

  • Alexey
    Alexey
    Newbie 10 points
    February 28, 2019 at 11:26 pm

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

  • Marie
    Marie
    Super Guru 1,747 points
    March 1, 2019 at 7:40 am

    hi Alexey,

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

  • JamesD
    JamesD
    Super Guru 1,345 points
    March 1, 2019 at 9:19 am

    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');
  • Chris
    Chris
    Newbie 36 points
    March 30, 2019 at 1:20 pm

    @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?

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

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

  • JamesD
    JamesD
    Super Guru 1,345 points
    March 31, 2019 at 12:51 am

    @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.

  • Chris
    Chris
    Newbie 36 points
    April 2, 2019 at 7:50 am

    @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!

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

    @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

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

    @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.

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

    disregard

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

    @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.

  • JamesD
    JamesD
    Super Guru 1,345 points
    April 4, 2019 at 6:30 am

    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.

Viewing 14 posts - 1 through 14 (of 14 total)