1

I have two select created using ACF Pro in a custom post type. I made an archive with a wordpress loop to display those element but depending on the order I declare it, my variable it returns : null. Also if I put it before the_title() witch in case of wp_loop should return the title of post type it return the title of the page.

$stagesId = get_field("stages");
$jobsId = get_field("job");
var_dump($stagesId);
var_dump($jobsId);

In the case it show : 3 and NULL

$jobsId = get_field("job");
$stagesId = get_field("stages");
var_dump($jobsId);
var_dump($stagesId);

In the case it show : 21 and NULL

$args = array(
        'post_type'      => 'catalogue',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order'          => 'DESC',
        'orderby'        => 'post_date'
    );

    $the_query = new WP_query($args);
    $jobs = array(); ?>
    <?php if ( $the_query->have_posts() ) : ?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php 
                $jobs[$the_query->post->post_title] = $the_query->post->post_title;

            ?>
            <a href="#contact_form?<?php echo get_the_title()  ?>" class="card--link">
                <div class="card">
                    <div class="card--header">
                        <h3><?php the_title() ?></h3>
                    </div>
                    <div class="card--body">
                        <p><?php the_field("content") ?></p>
                        <?php

                            $stagesId = get_field("stages");
                            $jobsId = get_field("job");
                            var_dump($jobsId);
                            var_dump($stagesId);
                        <p>Nombre de personne disponible pour ces jobs : <?php echo $count ?></p>
                    </div>
                </div>
            </a>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; 

Here are how I create my field

array(
            'key' => 'field_61386dc51b308',
            'label' => 'Job',
            'name' => 'job',
            'type' => 'select',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'choices' => array(
            ),
            'default_value' => array(
            ),
            'allow_null' => 0,
            'multiple' => 1,
            'ui' => 0,
            'return_format' => 'value',
            'ajax' => 1,
            'placeholder' => '',
        ),
        array(
            'key' => 'field_613f0c5df7a97',
            'label' => 'Stages',
            'name' => 'stages',
            'type' => 'select',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'choices' => array(
            ),
            'default_value' => array(
            ),
            'allow_null' => 0,
            'multiple' => 1,
            'ui' => 0,
            'return_format' => 'value',
            'ajax' => 1,
            'placeholder' => '',
        ),
Beatles BOB
  • 333
  • 1
  • 13
  • Without a post id explicitly specified via the second parameter, `get_field` looks for data on the "current post." Whether you have set up your WP loop to _set_ the current post correctly in the first place, we can't tell with the snippets you have shown so far. – CBroe Sep 13 '21 at 09:58
  • I edit it, tell me if it's not clear – Beatles BOB Sep 13 '21 at 10:01
  • Hm, calling `the_post` should normally set up the necessary things. What happens when you explicitly pass the post ID (`$the_query->post->ID`) to these function calls? – CBroe Sep 13 '21 at 10:07
  • Nothing change, I used get_the_ID() – Beatles BOB Sep 13 '21 at 10:09
  • Read this - https://www.advancedcustomfields.com/resources/select/ . – Snuffy Sep 13 '21 at 10:13
  • 1
    @MartinMirchev, Sorry Martin I dont get it, can you explain what you thinkis important, I'm really sorry i'm quite new using ACF PRO. The fact here, that's somehow I get null If there is a get_field before it for apparently no reason – Beatles BOB Sep 13 '21 at 10:17

1 Answers1

0

From the ACF documentation there are few settings to keep in mind. Do you want both label and value stored or one of them. Do we have default value ? Do we allow null ?

From the documentation Allow Null option - If selected, the select list will begin with an empty choice labelled “- Select -“. If using the Stylized UI setting, this choice will be replaced by a ‘x’ icon allowing you to remove the selected value(s).

Here is an example of a post loop with select field

get_header();
    if(have_posts()):
        while(have_posts()): the_post();
        // If select is array ( value and label)
        $color = get_field('color');
            if(isset($color['value'])):
                var_dump($color['value'].' '.$color['label']);
            endif;

        // If select is label or value
        $color = get_field('color');
            if(isset($color)):
                var_dump($color);
            endif;
        endwhile;
    endif;
get_footer(); 

For multi select field we need to loop our values.

get_header();
    if(have_posts()):
        while(have_posts()): the_post();
        echo '<div>';
        the_title();
        echo '</br>';
        $colors = get_field('colors');
        if(isset($colors)):
            echo implode(',',$colors); // or use foreach
            foreach($colors as $color):
               echo $color;
            endforeach;
        endif;
        echo '</br>';
        $sizes = get_field('sizes');
        if(isset($sizes)):
            echo implode(',',$sizes); // or use foreach
            foreach($sizes as $size):
               echo $size;
            endforeach;
        endif;
        echo '</div>';
        endwhile;
    endif;
get_footer(); 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Snuffy
  • 1,723
  • 1
  • 5
  • 9
  • 1
    Ok I do understand, the point here, this options aren't allowed in my field and they are not empty (required), there also no default value and if I only put get_field('stages') It's no null I get correctly the id, but when I put get_field('job') here i get my stages go to NULL – Beatles BOB Sep 13 '21 at 10:53
  • Can you share your json or screenshot the fields ? – Snuffy Sep 13 '21 at 10:58
  • I edit you post with the way I created my field, not sure why I did that :/ – Beatles BOB Sep 13 '21 at 11:04
  • From what i see your choises are empty ?!? Since you use multiple values you will get array so either use implode or foreach to loop all values. I have updated my answer – Snuffy Sep 13 '21 at 11:24
  • 1
    I populate it using acf/load_field, they populate correctly so this point seems to work fine – Beatles BOB Sep 13 '21 at 11:28
  • Is it possible that you have some code in your theme/plugin that is loading field values earlier than the "init" action? – Snuffy Sep 13 '21 at 13:06
  • No it's not, I try to explore this way but nothing and my select value on the backend are well and display all I want – Beatles BOB Sep 13 '21 at 14:42
  • Update when i get_field() from content who isn't a select field, It all seems to work – Beatles BOB Sep 14 '21 at 06:21
  • I dont think the problem is within get_field type. I think you should test on clean wp with default theme with only acf active to test. – Snuffy Sep 14 '21 at 06:34