4

I'm trying to make classified WordPress theme and I need to make users posts expire date plus show that date into the post. Users only allowed to publish their posts in post-types not as ordinary WordPress posts.

Does anyone know how can i make it?

Update: According to the answers given to this question till now I have to say giving expire date to the post types by meta post is not what I want cause users could change that I want the 30 days date (means 30 days after publish their post) the post permanently be deleted and the date automatically add to the post in back-end not manually with admin or author. Hope my mean be more clear now.

Special thanks to: @sakibmoon . Thanks.

  • What happens when a post "expires"? – s_ha_dum May 21 '15 at 0:29
  • be delete or going to draft no matter just not appear in website anymore – Scott Wolter May 21 '15 at 0:32
6

You can set the expire date as post meta value. In single.php you can easily show that date by querying the post meta.

See update_post_meta() and get_post_meta()

Now, the second part of your question is pretty tricky. I can think of 2 solutions.

  • Cron

You can use wordpress cron to run at a time interval to check which post has been expired and delete/set as draft/whatever you wan't to do with those posts. See wp_cron()

Pros:

  1. Very easy to implement.

Cons:

  1. The cron task won't fire, if no one visit your site.(For busy site with frequent user, this is not a issue)

  2. If you have lots of post, checking every post at a time interval will have performance penalty.

  3. Not reliable at all. The post will not expire exactly at the time specified.

( I have always hated wp_cron() and never use it myself)

A little bit better option is to use cron task from your server. But even this won't ensure post expiry at exact time as you have to run cron at a time interval and also there is performance penalty.

  • Modify Single.php file and Post Query

A better option in my opinion is to check post expiry date in your single.php and display a message checking expiration_date. You can also delete/set as draft, first time this page is requested by a user after the expiration date causing 404 for further visit.

To remove the expired pages from archive pages, you can use pre_get_post action hook to modify the query based on the post meta.

Pros

  1. Exact expiration date.
  2. Total control over what you want to do.
  3. Negligible performance penalty.

Cons:

  1. Just a little bit more work.

(Considering the advantages, it's not that much of more work. If I were you, I would choose this option)

  • Use a Plugin

There are lots of plugins that seems to do this exactly. But I don't know how they work or whether they can fulfill your requirements. You have to check these yourself.

EDIT

I'm trying to make classified WordPress theme and I need to make users posts expire date plus show that date into the post. Users only allowed to publish their posts in post-types not as ordinary WordPress posts.

You have to use register_post_type() to create custom post type. You will also need to create custom taxonomy if you want to categories them. Use register_taxonomy() for that.

Also see Is There a Difference Between Taxonomies and Categories?

According to the answers given to this question till now I have to say giving expire date to the post types by meta post is not what I want cause users could change that I want the 30 days date (means 30 days after publish their post) the post permanently be deleted and the date automatically add to the post in back-end not manually with admin or author.

Here's what you are wrong. Saving post meta isn't done by back end. How you are going to create and update post meta is up to you. For your case, it will be best to use save_post action hook and update the expire_date from there. A sample example would be

//This hook will fire when a post is created or updated.
add_action('save_post', 'set_expire_date_meta')

function set_expire_date_meta( $post_id ) {
    //We are checking if the saved post belongs to the CPT
    if( 'custom-post-type-name-that-you-registered' == get_post_type( $post_id ) ) {
        $meta = $get_post_meta($post_id, 'expire_date', true);
        //If the meta already exists, the post is being updated and we don't need to save the meta again
        if( $meta == '' ) {
            $timestamp_30day = strtotime("+1 month");
            update_post_meta( $post_id, 'expire_date', $timestamp_30day);
        }
    }
}

As you want the expiry date to work for the custom post type only, add an extra condition on main query like is_post_type_archive( 'custom-post-type-name-that-you-registered' ) in remove_expired_post_from_main_query function that I mentioned so that main query won't be altered for any other post type like post/page etc.

In expire_posts_to_404_redirect, change is_single() to is_singular('custom-post-type-name-that-you-registered') so that only these custom post type will go to 404.

//This filter will show 404 page for expired posts for the cpt that you registered
add_filter('template_redirect', 'expire_posts_to_404_redirect' );
function expire_posts_to_404_redirect() {
    global $wp_query;
    global $post;

    if( is_singular('custom-post-type-name-that-you-registered') ) {
        $expire_date = get_post_meta($post->ID, 'expire_date', true);
        if( ($expire_date != '') && ($expire_date < time()) ) {
            $wp_query->set_404();
            status_header( 404 );
        }
    }
}

//This action will alter main query to remove expired posts from archive pages of the CPT that you registered
add_action('pre_get_posts', 'remove_expired_post_from_main_query');
function remove_expired_post_from_main_query($query) {
    if( $query->is_main_query() && !is_admin() && is_post_type_archive('custom-post-type-name-that-you-registered') ) {
        //We are comparing expiry date with current timestamp.
        //It will only keep the posts with expiry_date > current timestamp
        $meta_query = array(
            array(
                'key' => 'expire_date',
                'value' => time(),
                'compare' => '>'
            )
        );
        $query->set( 'meta_query', $meta_query );
    }
}

Hope this helps.

  • thanks a lot now I'm lighting about the solution but can you give an example of your second opinion please? so maybe I can edit it to what i want? – Scott Wolter May 22 '15 at 3:45
  • @ScottWolter I've edited my answer. – sakibmoon May 22 '15 at 5:55
  • Thank you so much i going to test this code. Can I use this code in my plugin function page? cause for making post-type I'm using plugin. – Scott Wolter May 22 '15 at 15:09
  • @ScottWolter I've further edited my code. If you have any more questions let me know. If this solves your issue, accept the answer. – sakibmoon May 22 '15 at 19:06
  • your second code will bring my site into error page! says this line of code has problem: if( $query->is_main_query() && !is_admin() ) ) { while i want log in. 1- in your code second part has one ) more (after delete that still has error) 2- this is screenshot of my error postimg.org/image/f7gh3n44r – Scott Wolter May 23 '15 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.