Support

Account

Forum Replies Created

  • 
     $args = array( 
        'post_type' => 'product', 
        'post_status' => 'publish',
        'posts_per_page'  => -1,
      );
    
      $listings = new WP_Query( $args );
      $count = 0;
      $today = date( 'Ymd' );
      global $product;
    
    // The Loop
    if ( $listings->have_posts() ) {
        while ( $listings->have_posts() ) {
            $listings->the_post();
            $count = 0;
        $repeater = get_field('pasirinkti_datas',$listings->ID);
        if (is_array($repeater)) {
          $count = count($repeater);
        }
    
        echo $count. '</br>';
        if( have_rows('pasirinkti_datas', $listings->ID) ):
          while( have_rows('pasirinkti_datas', $listings->ID) ) : the_row();
          
              $value = get_sub_field('keliones_datos', $listings->ID);
              if($value < $today) {
                  $row = get_row_index();
                  delete_row('pasirinkti_datas', $row, $listings->ID);
              }
          endwhile;
      endif;
        }
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    This is the working code i guess?

  • Got it. But now it deletes all the rows, i think i write bad IF function or delete_row

    
      $args = array( 
        'post_type' => 'product', 
        'post_status' => 'publish',
        'posts_per_page'  => -1,
      );
    
      $listings = new WP_Query( $args );
      $count = 0;
      $today = date( 'Y-m-d' );
      global $product;
    
    // The Loop
    if ( $listings->have_posts() ) {
    	echo '<ul>';
    	while ( $listings->have_posts() ) {
    		$listings->the_post();
    		$count = 0;
        $repeater = get_field('pasirinkti_datas',$listings->ID);
        if (is_array($repeater)) {
          $count = count($repeater);
        }
    for ($row=$count; $row>0; $row--) {
      if (get_sub_field('keliones_datos',$listings->ID) <= $today ) {
        delete_row('pasirinkti_datas', $row,$listings->ID);
      }
    }
    echo '<li>' . get_the_title() .$count . '</li>';
    	}
    	echo '</ul>';
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
  • Thank you for information, i have more questions now:
    How i can querry and loop over all woocommerce products in functions.php?

    I created cronjob, made it run one time per day:

    
    add_action( 'triname_datas', 'triname_datas_func' );
    function triname_datas_func() {
      $today = date('Y-m-d');
      $repeater = get_field('pasirinkti_datas');
    // $repeater is an array of rows
    $count = count($repeater);
    for ($row=$count; $row>0; $row--) {
      if (get_sub_field('keliones_datos', $repeater) >= $today) {
        delete_row('pasirinkti_datas', $row);
      }
    }
    }
    

    But it is not working, i need to query and loop through products.

Viewing 3 posts - 1 through 3 (of 3 total)