I'm having problems querying posts in functions.php
. This is mainly to update posts in the back-end, not to show a query on the front end. I'm trying to get all posts that use the status post format and update a meta field if a variable is true.
My code:
function status_alerts($query) { //start function
global $post; // set the global
$args = array( // all posts in the status post format
'posts_per_page' => -1,
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-status' ),
'operator'=> 'IN'
);
$alert_query = new WP_Query( $args ); while ( $alert_query->have_posts() ) : $alert_query->the_post(); //query post
if (get_post_meta( $post_id, 'breaking_news_status', true ) == 'active') { // if the post has a meta field called 'active'
if ((get_post_meta($post_id, 'status_time_duration', true) + + get_the_time('U')) < date( 'U', current_time( 'timestamp', 0 ) )) { // if the 'status_time_duration' plus the publish date is greater than the current time
update_post_meta($post_id, 'breaking_news_status', 'archive'); // add a check to 'archive' to 'breaking_news_status'
}
}
endwhile;
}
I'm having problems querying posts in functions.php
. This is mainly to update posts in the back-end, not to show a query on the front end. I'm trying to get all posts that use the status post format and update a meta field if a variable is true.
My code:
function status_alerts($query) { //start function
global $post; // set the global
$args = array( // all posts in the status post format
'posts_per_page' => -1,
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-status' ),
'operator'=> 'IN'
);
$alert_query = new WP_Query( $args ); while ( $alert_query->have_posts() ) : $alert_query->the_post(); //query post
if (get_post_meta( $post_id, 'breaking_news_status', true ) == 'active') { // if the post has a meta field called 'active'
if ((get_post_meta($post_id, 'status_time_duration', true) + + get_the_time('U')) < date( 'U', current_time( 'timestamp', 0 ) )) { // if the 'status_time_duration' plus the publish date is greater than the current time
update_post_meta($post_id, 'breaking_news_status', 'archive'); // add a check to 'archive' to 'breaking_news_status'
}
}
endwhile;
}
Share
Improve this question
asked Jun 22, 2019 at 5:17
Gregory SchultzGregory Schultz
6236 silver badges31 bronze badges
5
|
1 Answer
Reset to default 1You have to replace $post_id with get_the_id();
function status_alerts($query) { //start function
global $post; // set the global
$args = array( // all posts in the status post format
'posts_per_page' => -1,
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-status' ),
'operator'=> 'IN'
);
$alert_query = new WP_Query( $args ); while ( $alert_query->have_posts() ) : $alert_query->the_post(); //query post
if (get_post_meta( get_the_ID(), 'breaking_news_status', true ) == 'active') { // if the post has a meta field called 'active'
if ((get_post_meta(get_the_ID(), 'status_time_duration', true) + + get_the_time('U')) < date( 'U', current_time( 'timestamp', 0 ) )) { // if the 'status_time_duration' plus the publish date is greater than the current time
update_post_meta(get_the_ID(), 'breaking_news_status', 'archive'); // add a check to 'archive' to 'breaking_news_status'
}
}
endwhile;
wp_reset_query();
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745379154a4625142.html
$post_id
is not defined in your code. Did you mean to use$post->ID
or maybe do$post_id = get_the_ID();
? – Sally CJ Commented Jun 22, 2019 at 6:09$post->ID
still didn't update. – Gregory Schultz Commented Jun 22, 2019 at 8:13get_post_meta()
would do the job; there's no need to usewp_update_post()
. Also, you could use meta query - add'meta_key' => 'status_time_duration', 'meta_value' => 'active'
to the$args
array to query for posts that have the metadatastatus_time_duration
set toactive
. That way, you wouldn't need theif (get_post_meta( $post_id, 'breaking_news_status', true ) == 'active')
. – Sally CJ Commented Jun 22, 2019 at 10:34add_action( 'init', 'status_alerts' );
– Gregory Schultz Commented Jun 22, 2019 at 21:19