Skip to content

Instantly share code, notes, and snippets.

@ruucm
Created December 21, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruucm/59cce22ba4690839cdfc3e2547357c33 to your computer and use it in GitHub Desktop.
Save ruucm/59cce22ba4690839cdfc3e2547357c33 to your computer and use it in GitHub Desktop.
add specific term to last 2 month posts (wordpress)
<?php
$args = array(
'post_type' => 'cartoon',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last 2 month
'date_query' => array(
array(
'after' => '2 month ago'
)
),
'posts_per_page' => -1
);
$query = new WP_Query( $args );
$found_posts = $query->found_posts;
if ($found_posts > 0) {
$posts = $query->posts;
for ($i=0; $i < sizeof($posts); $i++) {
wp_add_object_terms( $posts[$i]->ID, 'new', 'new_or_popular' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment