Support

Account

Home Forums General Issues Get nothing when update_field from functions.php but in admin everything is ok

Solved

Get nothing when update_field from functions.php but in admin everything is ok

  • functions.php:

    <?php
    function prefix_new_review() {
      $my_post = array(
      	'post_title'=> 'review',
    	'post_type' => 'reviews_category',
      	'post_status' => 'publish'
      );		
      $post_id = wp_insert_post( $my_post );
      $text = '123';
      update_field( 'text', $text, $post_id );
    		  
    }
    add_action( 'admin_post_nopriv_new_review', 'prefix_new_review' );
    add_action( 'admin_post_new_review', 'prefix_new_review' );
    ?>

    My text is shown in admin, but when i query it from page i get nothing:

    <?php
        $args = array(
            'post_type' => 'reviews_category',
            'posts_per_page' => 10
        );
        $the_query = new WP_Query( $args );
    ?>
    
    <?php if( $the_query->have_posts() ): ?>
        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php $id = get_the_ID(); ?>
            <?php the_field('text', $id); ?>
        <?php endwhile; ?>
    
    <?php endif; ?>
                    
    <?php wp_reset_postdata(); ?>
        
  • When i change text in admin and update post it’s shown good. so problem in update_field(i think)

  • Solved by https://support.advancedcustomfields.com/forums/topic/get_field-doesnt-return-a-value-when-fields-are-created-with-update_field/

    How solved it:

    • go to mysql(phpmyadmin)
    • table = wp_postmeta
    • find recently edited data
    • get like – field_34tgwer data and replace field_name

    update_field( 'field_58538261203ed', $name, $post_id );

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

You must be logged in to reply to this topic.