Support

Account

Home Forums Front-end Issues ACF data not showing after Ajax call

Solved

ACF data not showing after Ajax call

  • I’ve set up a simple filter using Ajax so that the user can filter prices high to low.

    The issue I’ve got is that my when the ajax call runs the data returns but is missing all the ACF fields.

    This is my PHP code;

    function cars_filter_function(){
    
        $args = array( 
            'post_type'         => 'cars',
            'posts_per_page'    => -1,
        );
    
    	$args = array(
    		'orderby' => 'date', // we will sort posts by date
    		'order'	=> $_POST['sort_price'] // ASC or DESC
    	);
    
    	$query = new WP_Query( $args );
    	
    	if( $query->have_posts() ) :
    		while( $query->have_posts() ): $query->the_post();
                include(TEMPLATEPATH . '/includes/cars.php');
    		endwhile;
    		wp_reset_postdata();
    	else :
    		echo 'No posts found';
    	endif;
    	
    	die();
    }

    Any ideas, please?

  • @forbiddenchunk

    You say data is returned, so I’m assuming title, permalink and post ID return ok?

    What does cars.php include?

    Do you declare your ACF fields on the template?

    From memory, I think I had similar from an ajax call, does it work if you change your include to:

    include( locate_template( 'includes/cars.php', false, false ) );

  • Hi @jarvis,

    Thanks for replying, I managed to solve it using

    get_field('tax_amount', $post_id)

    and making sure the following was at the top;

    `global $post;

    $post_id = false;
    if (defined(‘DOING_AJAX’) && DOING_AJAX) {
    if (isset($_GET[‘post_id’])) {
    $post_id = $_GET[‘post_id’];
    }
    } else {
    $post_id = $post->ID;
    }`

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

You must be logged in to reply to this topic.