Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Hur man sorterar posts efter postens löptid i WordPress

Tidigare har vi delat med oss av hur man löper ut tillägg i WordPress med hjälp av Post Expirator Plugin. När vi skapade en site för listning av händelser fann vi att detta plugin var till stor hjälp. Vi kan enkelt radera de händelser som löper ut. För det andra, tack vare detta plugin, är det verkligen lätt att sortera inlägg efter postens utgångsdatum också. I den här artikeln kommer vi att visa dig hur du sorterar inlägg efter inläggets utgångsdatum i WordPress.

Code Updated för att återspegla ändringarna i plugin där de ändrade namnet på det anpassade fältet. Thank Tajim för att du lät oss veta i kommentarerna.

I vårt vissa projekt hade vi Händelser som en Custom Post Type. Nu är detta för en ideell organisation som gillar ett evenemang per månad, så vi skapade helt enkelt en slinga för att lägga in på deras events page. Du kan använda koden i din sidebar, eller var du vill.

<?php
$args = array( 'post_type' => 'event', 'posts_per_page' => 15, 'order' => 'ASC', 'meta_key' => '_expiration-date', 'orderby' => 'meta_value' );
$eventloop = new WP_Query( $args );
if ( $eventloop->have_posts() ) : while ( $eventloop->have_posts() ) : $eventloop->the_post();

//All the Loop Content Goes Here

endwhile;
endif;
?>

Tricket är att använda meta_key och sedan användare oderby meta_value. Att sätta den i stigande order visar den händelse som kommer snart (löper ut tidigast). Exempel på vår lista över händelser:

Sort Posts by Post Expiration (Event Example)

Obs/observera att för att detta ska fungera MÅSTE du använda Post Expirator Plugin.

Om du har några frågor är du välkommen att fråga i kommentarerna under.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

24 kommentarerLämna ett svar

  1. Francis Waller

    Hi, I’m trying to get this work on my woo commerce products which have to manipulated to be offsite courses which expire on a certain date. I really need to sort them by expiry date not sure what I’m doing wrong. Thanks

  2. dtp

    Is it possible to show the posts that end till next day or next week, month and so on.
    I want to create a site thats shows all posts that are ending next day. and one which is shwoing all post that are ending next week.

    Any suggestions?

  3. James Parkin

    I have been trying to get this code to work with a Genesis Pro child theme. Made several posts with future dates. Add this code to sidebars via short codes and tried inside the functions.php file. Not using any custom fields or adding info.

    Any ideas would be appeciated

  4. James

    I have been trying to get this to work to no avail. posted it in sidebar, function.php and as short codes. Any help would be appreciated.

  5. James

    I have not been able to get this to work with a Genesis Pro child theme. Added it to the last lines of the function.php file. added meta values too. unsure why it does not post in ASC order by expiration date.

    thank you in advanced

  6. JP

    Posts like this are why I love the internet (and blogs like yours). This is exactly what I was looking for and couldn’t have been laid out more simply. Thanks!

  7. Dan

    Just what I was looking for – thanks!

  8. Tajim

    Just wanted to say that in current version of this plugin the custom field name has changed from
    expiration-date to _expiration-date.

    The custom field is hidden. So please update the code above.

    Regards
    Tajim

  9. AMBA JUNIOR

    Thanks for this. Was wondering how you added the date images to the posts

    • Editorial Staff

      We pulled the meta information that this plugin stores in the post custom fields. Then styled it. That was a background CSS image and text overlay.

      Administratör

      • Amba Junior

        Thanks for the feedback. Would it be OK to get a sample code from you on this? Thanks in advance.

        • Editorial Staff

          Unfortunately, we don’t offer that as part of our free service. You are more than welcome to hire us to write a custom snippet for you.

  10. Steve Combes

    Thanks for the great post.

    If I wanted to echo the post expiration date in the loop what code might I use?

    • Editorial Staff

      You would need to use the get_post_meta function and call the meta key ”expiration-date”

      Example would be something like this:

      $expdate = get_post_meta($post->ID, 'expiration-date', true);
      echo $expdate;
      

      Administratör

      • Adam

        I changed it to ’_expiration-date’ and I’m getting the data, but how would you re-sort this into the correct date display? My date is March 1st 2015 and what’s echoed is ”1425254340”

        Thanks,

  11. Kitsts

    So for a total beginner, where would I put this code for it to sort by expiration?

    • Editorial Staff

      This article is not for total beginners. Because you have to have fair understanding of how WordPress themes work in order to make this code work with your specific theme.

      Administratör

      • CuriousObserver

        If this isn’t for beginners, why did you post it to wpbeginner.com? I, too, am having difficulty inserting this into my blog and would like to see an answer to KITSTS’ question.

        • Editorial Staff

          Here at WPBeginner, we define beginners into various levels. Beginner blogger aka total beginner (just starting to blog, doesn’t know what is RSS, FTP etc). Beginner theme designer (knows HTML+CSS, but has no idea how to build themes in WordPress). Beginner plugin developer (knows the general concept around programming, but not specific to WordPress).

          The reason why we can’t answer KITST’ question is because it is impossible to answer with the information provided. The code shared in the article is a standard WordPress loop. Where you paste it in your theme will vary based on your needs and also the type of theme you are using.

          If you are using a child theme, then most likely that code will end up in your functions.php file, so you can hook it into the function provided by the theme framework.

          If you are using another WordPress theme, then the code can go in various different template files (index.php, sidebar.php, archive.php, archive-{posttype}.php, footer.php, header.php, etc). Each theme is organized differently which makes it impossible for us to give you an exact location where to paste this code.

          Also note, that the loop that we shared above is from an example work that we created using a custom post type called events. You may not want to use it for a custom post type events. Perhaps you want to use it for a different post type called deals.

          This article was intended for beginner theme designers and/or beginner developers who are looking for an easy way to do this.

  12. G J

    I was wondering, which plugin is used for displaying the upcoming events in the example above?
    Thanks for a lovely article…

    • Editorial Staff

      That wasn’t a plugin. It was just custom styling a custom post type display using the loop code that we shared in this article.

      Administratör

      • G J

        Ahhh..ok, is it possible for you to give an example on ”All the Loop Content Goes Here” I am updating the webpage of a NGO and kind of liked this way of displaying the events on. As you may have noticed I am a beginner so I would really appreciate your help ;o)….

        • Editorial Staff

          The loop code essentially is the_title(); php tag to show the title. Another custom field to show the date. The loop is explained pretty well here:

          http://codex.wordpress.org/The_Loop

          We used styling (div/images/css) to make it look that way. This post is about how to sort posts by expiration date, and the article shows just that. The example used is something that we did for our client, so unfortunately we cannot share the images/styling that they paid for. Send us an email if you are interested in having us do this for you. We have special non-profit rates.

  13. Jacko

    This is a cool feature.  There are several situations where this is what you need to be doing.
     
     
    PS
    This website has one of my favorite designs.  The #IBCT is about finding the best blogger in the world. Your website has made it to the semis.

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.