1

I'm trying to do a WP_query on a custom-post-type but for some reason I cannot get the values from the custom-field-types of these posts.

Here is what I got so far (functions.php)

function fetch_cases(){

$args = array(
    'post_type' => array('case'),
    'post_status' => array('publish'),
    'posts_per_page' => 5
);


$query = new WP_Query($args);
if($query->have_posts()) {
    while($query->have_posts() ){
        $query->the_post();
        ?>
       <a href="<?php the_permalink(); ?>">
            <div style="background-image:url('<?= get_field('case_picture'); ?>')">
                <p><?= get_field('case_title') ?></p>           
            </div>
        </a>
    <?php }
}
die();

add_action('wp_ajax_nopriv_fetch_cases', 'fetch_cases');
add_action('wp_ajax_fetch_cases','fetch_cases');
}

And in my JS file have the following:

$.ajax({
  url: "/wp-admin/admin-ajax.php",
  data: {
    action: "fetch_cases"
  },
  success: function(data) {
    $(".fetch_cases").append(data);
  },
  error: function(errorThrown) {
    console.log(errorThrown);
  }

});

Can someone tell me what I'm doing wrong?

I also tried to do:

<?php the_field('case_picture'); ?>

but with no luck? what am I missing?

3 Answers 3

1

add_action() should be outside your custom function. Try this instead.

function fetch_cases(){
    $args = array(
        'post_type' => array('case'),
        'post_status' => array('publish'),
        'posts_per_page' => 5
    );

    $query = new WP_Query($args);

    if($query->have_posts()) {
        while($query->have_posts() ){
            $query->the_post();
            ?>
           <a href="<?php the_permalink(); ?>">
                <div style="background-image:url('<?= get_field('case_picture'); ?>')">
                    <p><?= get_field('case_title') ?></p>           
                </div>
            </a>
        <?php }
    }
    die();
}

add_action('wp_ajax_nopriv_fetch_cases', 'fetch_cases');
add_action('wp_ajax_fetch_cases','fetch_cases');
Sign up to request clarification or add additional context in comments.

Comments

0

you can use this logic by storing the field as a hidden value and pass in ajax through js

$query = new WP_Query($args);
if($query->have_posts()) {
    while($query->have_posts() ){
        $query->the_post();
        ?>
       <a href="<?php the_permalink(); ?>">
            <div style="background-image:url('<?= get_field('case_picture'); ?>')">
                <p><?= get_field('case_title') ?></p> 
                <input type="hidden" id="hidden" name="hidden_field" value="<?= get_field('case_picture'); ?>"> // store the value
            </div>
        </a>
    <?php }
}
die();

Now get the data in jquery and pass through ajax

<script>
    var hidden=//Grab data here.
    $.ajax({
  url: "/wp-admin/admin-ajax.php",
  data: {
    action: "fetch_cases",
    image:hidden, // Pass the data
  },
  success: function(data) {
    $(".fetch_cases").append(data);
  },
  error: function(errorThrown) {
    console.log(errorThrown);
  }
});
</script>

and use the data in ajax called

function fetch_cases()
  {
      $image=$_POST['image'];
  }

Comments

0

get_field method has 2nd parameter which is post ID, pass this and check. It should work.

  $post_id = $post->ID;
  $value = get_field( 'case_picture', $post_id );

4 Comments

do you get case_title value ?
rename the field name may be same field name is used anywhere which you forgot.
No, like the questions reads I do not get any values from the custom fields
Looks like field name or any other plugin is conflict. I use same code but use only custom field and woocomerce plugin and its working properly. I prefer you to create new temporary group and give some different name and check with that. if not working then de-activate the plugins step by step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.