Support

Account

Home Forums General Issues Publish posts based on ACF date picker field

Unread

Publish posts based on ACF date picker field

  • Hi there. Longtime ACF user here. I’m trying to create two functions, one to publish posts based on ACF date picker field, the other to expire them on a date picker field. I’ve looked at all the forum topics on this and tried most of the existing code out there, yet can’t get anything to work.

    For examples using crons and not transients, the cron is firing correctly and no errors are logged in the error log. Here’s the code tried on that

    add_action( 'wp', 'publish_coupons_yo' );
    
    function publish_coupons_yo () {
        if ( ! wp_next_scheduled( 'publish_coupon_function' ) ) {
            wp_schedule_event( time(), 'hourly', 'publish_coupon_function'); //hourly for testing
        }
    }
    add_action( 'publish_coupon_function', 'publish_coupon_function_callback' );
    
    function publish_coupon_function_callback() {
    
        $date = date('Ymd'); // matches the date in DB
        $args = array(
            'post_type' => 'coupon', 
            'posts_per_page' => -1,
            'post_status' => 'draft',
            'meta_query' => array( 
              'relation' => 'OR',
                array (
                   'key'     => 'publish_coupon_date',
                   'value' => $date, 
                   'compare' => '=' 
                ),
              array (
                'key' => 'publish_coupon_date',
                'value' => $date,
                'compare' => '<'
              )
            ) // end meta_query
          ); // end $args
    
    $publishquery = new WP_Query($args);
      if ($publishquery->have_posts()) {
        global $post;
        if( !is_object($post) ) 
         return;
        while ($publishquery->have_posts()) {
          $publishquery->the_post();
          wp_transition_post_status('publish', 'draft', $post->ID);
        } // end while have_posts
        
        wp_reset_postdata();
      } // end if have_posts
    } // end function
    

    For the transients example in the ACF forums here and elsewhere on the web, I have the same problem as this guy who posted on stackexchange, and a person who commented on the Gist of that same code, namely that it throws a PHP Notice: Trying to get property ‘ID’ of non-object and PHP Notice: Trying to get property ‘post_type’ of non-object.

    Wondering if the code in these examples dating 2016 etc are out of date, since they used to seem to work for people but more recently are not.

    I’ve verified that the date output matches the server output format, so that’s not the issue. Fairly stumped. Any thoughts?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.