0

I'm a new user of Stack Overflow and have very basic PHP knowledge.

I am coming across the PHP notice of Trying to get property 'ID' of non-object

I am using PHP 7.3, WordPress and ACF to set a custom post type to set a post status to draft after my closing date has past.

I'm getting the error in the error_log in public_html

PHP Notice: Trying to get property 'ID' of non-object in public_html/assets/themes/gig/functions.php on line 979
if ($expireTransient = get_transient($post->ID) === false) {

PHP Notice: Trying to get property 'ID' of non-object in public_html/assets/themes/gig/functions.php on line 980
set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );

The code is set in my functions.php file within my WordPress theme folder, I do not have PHP logging enabled

Can anyone help to correct the PHP notice

if ($expireTransient = get_transient($post->ID) === false) {
    set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
    $today = date('Ymd', current_time('timestamp', 0));
    $args = array(
        'post_type' => 'vacancies',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'closing_date',
                'value' => $today,
                'compare' => '<='
            )
        )
    );

    $posts = get_posts($args);

    foreach( $posts as $post ) {
        if(get_field('closing_date', $post->ID)) {
            $postdata = array(
                'ID' => $post->ID,
                'post_status' => 'draft'
            );

            wp_update_post($postdata);
        }
    }
}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
karkey
  • 1
  • 3
  • 2
    Where are you getting the error? What line? The first line of the posted code have `$post->ID`. I can see that you define it later, in your `foreach`-loop, but not anywhere before that. Where does `$post` come from and what does it contain? – M. Eriksson May 22 '19 at 14:40
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – aynber May 22 '19 at 14:50
  • Added additional information to the post @MagnusEriksson hope this is the way the community works. Thank you for replying. – karkey May 22 '19 at 14:58
  • I need to ask again: _"Where does `$post` come from and what does it contain?"_ Before your if-statement, do a `var_dump($post);` to check what it actually contains. You still haven't answered where that variable comes from. Also, is this code in a function or something? We need more context. – M. Eriksson May 22 '19 at 15:02
  • $post does not exist. Please show any code you have which sets the $post variable. – mediaguru May 22 '19 at 15:12
  • 1
    Could you update this post with the full code in question? This code will need to be fired from within an action or filter. The issue here is that the $post object is not passed to this action or filter. – Marc May 22 '19 at 16:15

0 Answers0