0

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;
}
  • 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 Jun 22 '19 at 6:09
  • changed to $post->ID still didn't update. – Gregory Schultz Jun 22 '19 at 8:13
  • Never mind, used the wrong function. Should have used ‘wp_post_update’ – Gregory Schultz Jun 22 '19 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 Jun 22 '19 at 10:34
  • 1
    And I left out add_action( 'init', 'status_alerts' ); – Gregory Schultz Jun 22 '19 at 21:19
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();
}
| improve this answer | |
  • changed to $post->ID, still didn't update the post – Gregory Schultz Jun 22 '19 at 8:07
  • 1
    try get_the_ID() instead of $post->ID as i mentioned above code . – Chetan Vaghela Jun 22 '19 at 8:30
  • You left out add_action( 'init', 'status_alerts' ); I corrected your answer. Thanks again – Gregory Schultz Jun 22 '19 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.