query posts in functions.php and update a field

I'm having problems querying posts in functions.php. This is mainly to update posts in the back-end, not to show a

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
  • 2 The $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
  • changed to $post->ID still didn't update. – Gregory Schultz Commented Jun 22, 2019 at 8:13
  • Never mind, used the wrong function. Should have used ‘wp_post_update’ – Gregory Schultz Commented Jun 22, 2019 at 9:07
  • Ok, but if you're just updating a metadata and not other post fields, then get_post_meta() would do the job; there's no need to use wp_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 metadata status_time_duration set to active. That way, you wouldn't need the if (get_post_meta( $post_id, 'breaking_news_status', true ) == 'active'). – Sally CJ Commented Jun 22, 2019 at 10:34
  • 1 And I left out add_action( 'init', 'status_alerts' ); – Gregory Schultz Commented Jun 22, 2019 at 21:19
Add a comment  | 

1 Answer 1

Reset to default 1

You 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

相关推荐

  • query posts in functions.php and update a field

    I'm having problems querying posts in functions.php. This is mainly to update posts in the back-end, not to show a

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信