I have a ACF
custom field with a selectbox and now I want to return the selected options with AJAX, so in my functions.php
-file I wrote this:
function get_movies(){
$posts = get_field('choose_movies');
if( $posts ): ?>
<?php foreach( $posts as $p ): ?>
<div class="movie-item">
<a href="<?php echo get_permalink( $p->ID ); ?>">
<div class="movie-wrapper">
<?php $image = get_field('movie_image', $p->ID); ?>
<div class="movie-image" style="background-image:url('<?php echo $image['url']; ?>')"></div>
<div class="content">
<p><?php the_field('movie_title', $p->ID); ?></p>
</div>
</div>
</div>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php }
add_action('wp_ajax_nopriv_get_movies', 'get_movies');
add_action('wp_ajax_get_movies','get_movies');
and in my js file when I call:
$.ajax({
url: "/wp-admin/admin-ajax.php",
data: {
action: "get_movies"
},
success: function(data){
console.log('DATA: ', data)
}
the data
is empty e.g. gives me 0
- can someone tell me what I'm doing wrong?
EDIT
OK, so I added the following lines to pass the postID:
function get_movies( $post ){
global $wpdb;
$pid = $post->ID;
$posts = the_field('choose_movies', $post->ID);
...
}
but still, $posts
is empty - this is really getting a pain in the a..