• I want automatically delete post if after a period of time, the post view count is lower than a given number but i don’t know more about php So i hope someone can help me!

    Here is what i figured out:

    [ Moderator note: Code fixed, please wrap code in backticks or use the code button. ]

    function del_post($post_id=0)
    {
    	$count = 0;
    	$post_id = !$post_id ? get_the_ID() : $post_id;
    	if ($post_id) {
    		$meta_count_key = 'my_views_count';
    		$count = get_post_meta($post_id, $meta_count_key, true);
    		if ($count == '') {
    			delete_post_meta($post_id, $meta_count_key);
    			add_post_meta($post_id, $meta_count_key, '0');
    		}
    
    		$count = intval($count);
    	}
    
    		if ($count < 10) {
            $post_date = strtotime($post->start_date);
            if(((time() - $post_date) > (7 * 24 * 60 * 60))) {
            wp_delete_post($post->ID);
            }
    		}
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • longtran295

    Here’s some code thats going to auto delete the posts whose ‘my_views_count’ is less than 10, which were published on or before 30 days ago. I’ve also made sure it does that in batches of 10 posts per iteration which can be increased by changing the value of posts_per_page. The function will execute once everyday and will do that with the help of WP Cron

    add_action('init', 'daily_auto_delete_scheduler');
    
    function daily_auto_delete_scheduler() {
        $timestamp = wp_next_scheduled('daily_auto_delete_posts');
        if ($timestamp == false) {
            wp_schedule_event(time(), 'daily', 'daily_auto_delete_posts');
        }
    }
    
    add_action('daily_auto_delete_posts', 'auto_delete_posts');
    
    function auto_delete_posts() {
        $posts = new WP_Query(array(
            'post_status'=> 'publish',
            'meta_query'    =>  array(
                array(
                    'key'   =>  'my_views_count',
                    'value' =>  10,
                    'compare'   =>  '<',
                    'type'  =>  'NUMERIC'
                )
            ),
            'date_query'    =>  array(
                'before'    =>  '30 days ago'
            ),
            'fields'    =>  'ids',
            'posts_per_page'    =>  10
        ));
        if($posts->post_count > 0){
            if(is_array($posts->posts)){
                foreach($posts->posts as $post_id){
                    wp_delete_post($post_id);
                }
            }
        }
    }

    Just in case you need the code for incrementing the counter for the posts as well, here it is

    /*
     * Runs at the init of every page and checks when to add the counters
     */
    
    function hits_counter_logic() {
        if (is_single() && get_the_ID() > 0) {
            increment_counter(get_the_ID());
        }
    }
    
    add_action('wp', 'hits_counter_logic');
    
    /*
     * Adds the counter of the post
     */
    
    function increment_counter($post_id) {
        if ($post_id == null || $post_id < 1) {
            return;
        }
        $counter = get_post_meta($post_id, 'my_views_count', true);
        if (empty($counter)) {
            $counter = 0;
        }
        $counter = intval($counter);
        $counter++;
        update_post_meta($post_id, 'my_views_count', $counter);
    }

    Thread Starter longtran295

    (@longtran295)

    Thank u very much for your help. I thought that this thread has been forgotten.

    But i think i’ve done something wrong. I’ve copied the first code segment into theme/…/function.php file and install WP Crontrol, but when i run this cron, nothing happened although many post meet these rules and should be deleted.

    longtran295

    Welcome ??

    I forgot to mention that the function is not going to run until tomorrow, the first time that cron will be triggered is when its first scheduled to, which is tomorrow the same time.

    Though if you want to test out this function you can do something like

    add_action('init', 'testing_auto_delete');
    
    function testing_auto_delete() {
        if (isset($_GET['auto_delete']) && $_GET['auto_delete'] == 1) {
            auto_delete_posts();
        }
    }

    and the go to site with url like

    https://example.com/?auto_delete=1

    and it will run its first course

    Thread Starter longtran295

    (@longtran295)

    Thank u very much. Your codes are very awesome :)) d i hope the main code will work tomorrow

    Always welcome ??

    Its the effect of Coffee I guess.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to delete post if view-count y’ is closed to new replies.