Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Auto create a post every x days with different random custom taxonomy terms WordPress

  • SOLVED

I want to automatically create a custom post every 3 days. I want each generated post to be assigned a random term from 3 different custom taxonomies. I want each custom post title to be generated from these 3 random terms.

For example:
Custom taxonomy AAA has terms a,b,c,d,e,f,g
Custom taxonomy BBB has terms h,i,j,k,l,m,n
Custom taxonomy CCC has terms o,p,q,r,s,t,u

Each 3 days a custom post is created with a random title such as "a jumped over h to get to t"

Answers (4)

2019-12-04

Kyle answers:

//Create three day interval
add_filter( 'cron_schedules', 'add_cron_interval_three_days' );
function add_cron_interval_three_days( $schedules ) {

$schedules['three_days'] = array(
'interval' => 259200, //# of seconds in 3 days
'display' => esc_html__( '3 days' ),
);

return $schedules;

}

// Schedule Cron Job Event
function custom_cron_job() {
if ( ! wp_next_scheduled( 'three_day_hook' ) ) {
wp_schedule_event( time(), 'three_days', 'three_day_hook' );
}
}
add_action( 'wp', 'custom_cron_job' );

//Insert post action
function create_post_w_random_terms() {

$post_id = wp_insert_post( array(
'post_type' => 'post', //if this is a custom post type change this
'post_status' => 'publish',
'post_title' => '', //Set this to whatever you want
'post_content' => '',//Same here
));

$term_1 = array_rand( get_terms( array('taxonomy_1' => 'post_tag','hide_empty' => false,) ), 1 );
$term_2 = array_rand( get_terms( array('taxonomy_2' => 'post_tag','hide_empty' => false,) ), 1 );
$term_3 = array_rand( get_terms( array('taxonomy_3' => 'post_tag','hide_empty' => false,) ), 1 );

wp_set_post_terms( $post_id, $term_1, 'taxonomy_1' ); // change the third one to your taxonomy slug for all three
wp_set_post_terms( $post_id, $term_2, 'taxonomy_2' );
wp_set_post_terms( $post_id, $term_3, 'taxonomy_3' );

}
add_action( 'three_day_hook', 'create_post_w_random_terms' );


pjeaje comments:

What about the post title?


Kyle comments:

Meant to ask, unless you want all of them to be the same you'll probably want to drop in the date or something, so above the line with $post_id you could have $title = 'Your title text '.get_the_date( 'D M j' ); or whatever works for you.


Kyle comments:

I wouldn't use rempty's title either, as it will eventually generate duplicates


pjeaje comments:

Just add the post id on the end.


Kyle comments:

Okay, then you'll need to actually do it after you insert because the ID isn't generated yet, so going to post the full code.

//Create three day interval
add_filter( 'cron_schedules', 'add_cron_interval_three_days' );
function add_cron_interval_three_days( $schedules ) {

$schedules['three_days'] = array(
'interval' => 259200, //# of seconds in 3 days
'display' => esc_html__( '3 days' ),
);

return $schedules;

}

// Schedule Cron Job Event
function custom_cron_job() {
if ( ! wp_next_scheduled( 'three_day_hook' ) ) {
wp_schedule_event( time(), 'three_days', 'three_day_hook' );
}
}
add_action( 'wp', 'custom_cron_job' );

//Insert post action
function create_post_w_random_terms() {

$post_id = wp_insert_post( array(
'post_type' => 'post', //if this is a custom post type change this
'post_status' => 'publish',
'post_title' => ' ', //placeholder
'post_content' => '',//Same here
));

$my_post = array(
'ID' => $post_id,
'post_title' => 'This is the post title - '.$post_id,
);
wp_update_post( $my_post );

$term_1 = array_rand( get_terms( array('taxonomy_1' => 'post_tag','hide_empty' => false,) ), 1 );
$term_2 = array_rand( get_terms( array('taxonomy_2' => 'post_tag','hide_empty' => false,) ), 1 );
$term_3 = array_rand( get_terms( array('taxonomy_3' => 'post_tag','hide_empty' => false,) ), 1 );

wp_set_post_terms( $post_id, $term_1, 'taxonomy_1' ); // change the third one to your taxonomy slug for all three
wp_set_post_terms( $post_id, $term_2, 'taxonomy_2' );
wp_set_post_terms( $post_id, $term_3, 'taxonomy_3' );

}
add_action( 'three_day_hook', 'create_post_w_random_terms' );


pjeaje comments:

I see your point. Maybe the date then would be better.


Kyle comments:

This works fine, it is just an extra step, just added the couple lines of wp_update_post action.


Kyle comments:

Let me know if there is anything else, but that should do it


pjeaje comments:

How can I test it for every hour then revert back to x days


pjeaje comments:

Are you going to add the terms into the title?


pjeaje comments:

???

2019-12-04

Rempty answers:

Hello
try this one
add_filter( 'cron_schedules', 'cron_three_days' );
function cron_three_days( $schedules ) {
$schedules['three_days'] = array(
'interval' => 3 * 24 * 60 * 60, //73 days * 24 hours * 60 minutes * 60 seconds
'display' => __( 'Three Days', 'my-plugin-domain' )
);
}

if ( ! wp_next_scheduled( 'my_cronhook2' ) ) {
wp_schedule_event( time(), 'three_days', 'my_cronhook2' );
}

add_action( 'my_cronhook2', 'my_schedule_hook' );
function my_schedule_hook(){

/*Get the three terms 1 per taxonomy*/
$term1 = array_rand( get_terms( array('taxonomy' => 'post_tag','hide_empty' => false) ));/*replace post_tag with the correct taxonomy*/
$term2 = array_rand( get_terms( array('taxonomy' => 'post_tag','hide_empty' => false) ));/*replace post_tag with the correct taxonomy*/
$term3 = array_rand( get_terms( array('taxonomy' => 'post_tag','hide_empty' => false) ));/*replace post_tag with the correct taxonomy*/

/*Generate title*/
$title=$term1->name.' jumped over '.$term2->name.' to get to '.$term3->name;

$post_id = wp_insert_post( array(
'post_type' => 'post', /*You can change post to the correct post_type*/
'post_status' => 'publish',
'post_title' => $title,
'post_content' => ''
));

/*Set post terms per each taxponomy*/
wp_set_post_terms( $post_id, $term1->term_id, 'post_tag' );/*replace post_tag with the correct taxonomy*/
wp_set_post_terms( $post_id, $term2->term_id, 'post_tag' );/*replace post_tag with the correct taxonomy*/
wp_set_post_terms( $post_id, $term3->term_id, 'post_tag' );/*replace post_tag with the correct taxonomy*/

}


pjeaje comments:

Can you please check line 4 73 days?


Rempty comments:

It is 3 days it is just a misstype, also it is just a comment
3=days
24=hours
60=min
60=second

if you want to change to hourly
wp_schedule_event( time(), 'hourly', 'my_cronhook2' );
but first you must remove the current cron schedule
wp_clear_scheduled_hook('my_cronhook2');
just put the code load the page one time, then remove the above line and put the new wp_schedule_event hourly


pjeaje comments:

if you want to change to hourly
wp_schedule_event( time(), 'hourly', 'my_cronhook2' );

Don't I need to change this as well?
add_filter( 'cron_schedules', 'cron_three_days' );
function cron_three_days( $schedules ) {
$schedules['three_days'] = array(
'interval' => 3 * 24 * 60 * 60, //73 days * 24 hours * 60 minutes * 60 seconds
'display' => __( 'Three Days', 'my-plugin-domain' )
);
}


pjeaje comments:

What about a new post every 5 minutes? (so I can test it)


Rempty comments:

If you want 5 minutes
add_filter( 'cron_schedules', 'cron_five_minutes' );
function cron_five_minutes( $schedules ) {
$schedules['five_minutes'] = array(
'interval' =>5*60, //5minutes * 60 seconds
'display' => __( 'Five Minutes', 'my-plugin-domain' )
);
}
and change the schedule event to
wp_schedule_event( time(), 'five_minutes', 'my_cronhook2' );


pjeaje comments:

I installed the plugin "WP Crontrol" and it doesn't show any cron jobs associated with this? No posts are being published?

2019-12-04

Arnav Joy answers:

Are you still looking for help?

2019-12-06

pjeaje answers:

// change the third one to your taxonomy slug for all three

what does this mean?