• I’m trying to write a rudimentary plugin that will evaluate posts as they load. If the post has less than a certain word count, and is older than x days, I want my function to kick in and:

    a) Set up a 301 redirect to the post’s parent category, and
    b) delete the original post (so that it doesn’t show up in Archive listings.

    Can anyone recommend how to hook this into WordPress when the post is called?

Viewing 1 replies (of 1 total)
  • First of all the default wp post don’t have post parent assign to them, unless you are working with a custom post type that has the hierarchical argument set. Now assuming your post can have parent post, this should work:

    add_action('template_redirect', 'your_function');
    function your_function() {
    	if ( is_single() ) {
    		global $post;
    		$date1 = date_create( $post->post_date );
    		$now = ( date("Y-m-d", time() ) );
    		$date2 = date_create( $now );
    		$diff = date_diff( $date2, $date1 );
    		$days = $diff->format( '%a' ) ;
    		// change to the appropriate word count and days
    		if( ( str_word_count( $post->post_content ) >=  1253 ) && ( intval($days) >= 291 ) ) {
    			$category = get_the_category( $post->post_parent );
    			// first category
    			if( $category[0] ){
    				//wp_delete_post( $post->ID );
    				$link = get_category_link( $category[0]->term_id );
    				wp_redirect( $link, '301' ); exit;
    			}
    
    		}
    	}
    }

Viewing 1 replies (of 1 total)
  • The topic ‘What's the right hook to use as a post is loading?’ is closed to new replies.