Skip to content
Advertisement

How to get the meta value of the ACF field (wordpress)?

I need to get the meta value of the ACF field.
ACF field called ‘submitdate’ (format => Date Time picker : Y-m-d H:i:s) already has data ‘2021-06-11 17:23:36’
I tried the following code, but it only shows correct $post->ID, it doesn’t show $submitdate.
No error in console.
Would you please let me know how to echo the value of the field?

Query I tried:

$args = array(
'post_type'         => 'project',
'posts_per_page'    => -1,
'author'            => get_current_user_id(),
'name'              => get_the_title()
    );
$query = new WP_Query($args);

    if ($query->have_posts()) {
    global $post;
    while ($query->have_posts()) {
    $query->the_post();
    $submitdate = get_post_meta($post->ID, 'submitdate', true);
echo $post->ID;
echo $submitdate; 
  }
}

I also tried, it shows current date and time, not the value in the field:

$submitdate = get_field("submitdate", $post->ID);



Thank you.

Advertisement

Answer

/* I had same issue before few days and resolved with below function */

/* please try this line of code */
    
  $args = array(
    'post_type'         => 'project',
    'posts_per_page'    => -1,
    'author'            => get_current_user_id(),
    'name'              => get_the_title()
   );
  $query = new WP_Query($args);

    if ($query->have_posts()) {
    global $post;
   
    while ($query->have_posts()) {
         $query->the_post();
         $submitdate = get_field('submitdate', $post->ID ); // if changed field name then update key in this query

          echo $post->ID;
          echo $submitdate; 
     }
   }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement