Code to auto expire posts after 30 days

There are some issues with your query $daystogo = “30”; $sql = “UPDATE {$wpdb->posts} SET post_status=”draft” WHERE (post_type=”post” AND post_status=”publish”) AND DATEDIFF(NOW(), post_date) > %d”; $wpdb->query( $wpdb->prepare( $sql, $daystogo ) ); You do not want to untrash trashed posts by declaring them as draft, right? And you really do not want to make every automated … Read more

Update post date on every new comment?

From what i understand, you want to change the post’s modification time whenever a comment is left on your post. For this, you need to hook into the wp_insert_comment hook and update the post’s date manually: add_action(‘wp_insert_comment’,’update_post_time’,99,2); function update_post_time($comment_id, $comment_object) { // Get the post’s ID $post_id = $comment_object->comment_post_ID; // Double check for post’s ID, … Read more

Automatically added brs and paragraphs?

You can postpone the wp_autop filter. WordPress has this filter enabled by default. And it is processing before the shortcode output. remove_filter( ‘the_content’, ‘wpautop’ ); add_filter( ‘the_content’, ‘wpautop’ , 12); Add this to your functions.php and check if the problem persist! See a similar problem here: stray <p> elements

Update existing post dates to random dates

Question How to change the post date to a randomly generated date? Answer This is fairly straightforward. What I did was to create a plugin that upon activation get all the posts, loops through them, generates a random date, and updates the post with that date. <?php /** * Plugin Name: WPSE 259750 Random Dates … Read more

Prevent duplicate posts in wp_insert_post using custom fields

To save the link in the post meta you can use update_post_meta like this for example: $url = “http://sample.com/entertainment/default.aspx?tabid=2305&conid=102950” $my_post = array( ‘post_title’ => “$title”, ‘post_content’ => “$content”, ‘post_status’ => ‘draft’, ‘post_author’ => 1, ‘post_category’ => array(1), ); $post_id = wp_insert_post( $my_post ); update_post_meta($post_id,’source_link’,$url); and to prevent the insertion add a simple conditional check: $args … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)