Support

Account

Home Forums General Issues Looping through posts in a message field

Unread

Looping through posts in a message field

  • Hi,

    I’m trying to get the field value from all the posts of a custom post type, and display the list in the admin of this custom post type (the edit page of a single post), in an ACF message field.
    I actually have no trouble doing that, but the function causes some kind of conflict :
    My message is in a field group, and in a 2nd field group the values displayed are those of the first post of the CPT.
    In the 1st field group, no problem. If i move the message field to the 2nd field group, no problem in either group (but i need it in the first group)

    I tried everything for 3 hours, and have finally understood that it must be related to wp_reset_data();
    However, I tried that and it doesn’t work.
    I also tried $post_query->reset_postdata(); => this time it shows the most recent post of the query instead of the oldest.

    Here is my code (simplified at the maximum when i was trying to isolate the issue)

    <?php add_action('acf/render_field/key=field_61e184d76e05b', 'doublons_devis');
    
    function doublons_devis($field){  	
    
    	$args = array(
            'post_type' => 'reservation',
    		'posts_per_page' => '-1',
        );
    	
        $post_query = new WP_Query($args);
    
        if($post_query->have_posts() ) {
    		echo '<table>'; 
            while($post_query->have_posts() ) {
                $post_query->the_post();			
    
                ?>					 
    	<tr class=""><td>dummycontent</td></tr>
    	
    <?php		}
    $post_query->reset_postdata();
    		echo '</table>';
    	}
    		
     }?>

    Thank you in advance for your help…

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.