• Resolved George Stavropoulos

    (@zeus71)


    Hello.

    Is it possible the posts on category to be sorting by expiry date and not on post creation date.

    Thanks in advance.

    Regards,
    George

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Steve Burge

    (@stevejburge)

    Hi @zeus71

    Thanks for using PublishPress Future.

    It should be possible by clicking the “Expires” heading on the “Post” screen.

    Or am I not understanding your question 100% clearly?

    Thread Starter George Stavropoulos

    (@zeus71)

    Hello @stevejburge

    Thanks for your support.
    On backend works well, I mean on the frontend.

    Regards,
    George

    Plugin Author Steve Burge

    (@stevejburge)

    Thanks @zeus71

    We don’t have any frontend display controls, but we are looking to add this in upcoming releases.

    Thread Starter George Stavropoulos

    (@zeus71)

    Hello @stevejburge

    Is it possible to order posts by expirationdate with any hook on theme functions.php?

    Thanks in advance.

    Plugin Author Steve Burge

    (@stevejburge)

    Hi @zeus71

    We can’t help write this code, but do have some technical guidance available:
    https://publishpress.com/knowledge-base/technical-details/
    https://publishpress.com/knowledge-base/scheduling-cron-jobs/

    biboocl

    (@biboocl)

    Hello, you can do it with a custom loop, this example works for posts and products:

    <?php
        $args  = array(
          'post_type' => 'product', // or 'post'
          'posts_per_page' => '20',
          'post_status' => 'publish',
          'return' => 'ids',
          'orderby' => 'meta_value',
          'meta_key' => '_expiration-date',
          'order' => 'ASC', // or 'DESC'
          'product_cat' => 'yourcategory' // 'cat' => 'yourcategory'
      );
        $products = new WP_Query( $args ); 
        if ( $products->have_posts() ) {
          while ( $products->have_posts() ) : $products->the_post();
            wc_get_template_part( 'content', 'yourcontent' ); // or get_template_part( 'partials/content', 'post' ); for posts
          endwhile; // end of the loop.
          wp_reset_postdata();
        }
      ?>

    You can modify as you like for personalized content.
    Greetings 😉

    • This reply was modified 4 years ago by biboocl.
    Thread Starter George Stavropoulos

    (@zeus71)

    Hello @biboocl
    Thanks for your suggestion, but it didn’t work.

    Finally, I made it with the below snippet.

    // Short posts by expiry date
    
        function posts_sort_expirationdate( $query ) {
        if ( is_admin() || ! $query->is_category('your-category-slug') )
            return;
            $query->set(
                'meta_query', array(
                    'relation' => 'OR',
                    array(
                        'key' => '_expiration-date',
                        'compare' => 'EXISTS',
                    ),
                    array(
                        'key' => '_expiration-date',
                        'compare' => 'NOT EXISTS',
                        'value' => '',
                    ),
                )
            );
            $query->set('orderby', 'meta_value_num');
        	$query->set( 'order', 'ASC' );
            return;
    }
                                          
    add_action( 'pre_get_posts', 'posts_sort_expirationdate', 1 );
    

    Regards,
    George

    Plugin Author Steve Burge

    (@stevejburge)

    Thanks for sharing this @zeus71

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Sort Posts by expiry date’ is closed to new replies.