Support

Account

Home Forums Front-end Issues Custom field on post not accessible on archive page?

Solved

Custom field on post not accessible on archive page?

  • My problem seems similar to this post, but the solution is not working.

    I have posts with custom post types (either “volunteer” or “fundraising”)

    I created a custom field for posts of these types:
    * Label: ‘Visible on index page?’
    * Name: ‘visible_on_index’
    * Type: True/False
    * Default: True

    My goal is to filter the archive-volunteer.php and archive-fundraising.php pages so that they only show posts where ‘visible_on_index’ is True.

    I know I need to be inside the loop, and I am. I have successfully gotten the post ID and saved it to a variable. But still the field isn’t showing up. I’ve tried:

    • get_field('visible_on_index', $my_id);
    • get_post_field('visible_on_index', $my_id);
    • get_post_meta('visible_on_index', $my_id);

    … and anything else I can think of.

    So, I turned to get_fields($my_id) to see what is available, and sure enough, ‘visible_on_index’ isn’t there, but other ACF fields are.

    I’ve included some screenshots. All advice is welcome!

  • Have you added the field afterwards? Have you re-saved the posts then? Are the other fields coming from a different field group or are they in the same group?

  • Have you added the field afterwards?
    No, field was added before I started trying to use it on the archive page.

    Have you re-saved the posts then?
    Yes, I can see the field on the Admin side when I edit a post (and on the index view since I’m also using the ACF QuickEdit fields plugin).

    Are the other fields coming from a different field group or are they in the same group?
    They are in a different group. I tried creating the field in a group that *does* appear using get_fields(), but no change. I checked settings between this group (that doesn’t appear) and a group that does appear and all settings are the same.

  • Ok, I don’t know why, but it’s working as expected now.

    It must have been a cache issue, but I swear I was clearing the cache regularly.

    For the sake of closure, here’s the code that worked (once the field was accessible):

    
    <section class="center-align stories" id="stories">
      <div class="wrap cf">
        <div class="row">
          <?php
            if (have_posts()) : while (have_posts()) : the_post();
              $my_id = get_the_ID();
          ?>
            <?php // Only show posts where 'visibile_on_index' is not 0 (either 1 or unset)
              if (get_post_field('visible_on_index', $my_id) != 0) { ?>
              <div class="fourth <?php foreach(get_the_category() as $cat){ echo ' '.$cat->slug; }?>">
                <div class="inner">
                  <a href="<?php the_permalink(); ?>" class="img-holder" style="background-image: url(<?php the_post_thumbnail_url('full'); ?>)"></a>
                  <a href="<?php the_permalink(); ?>">
                    <h4><?php the_title(); ?></h4>
                  </a>
                  <?php the_excerpt(); ?>
                </div>
              </div>
            <?php }; ?>
          <?php endwhile; ?>
          <?php bones_page_navi(); ?>
          <?php endif; ?>
        </div>
      </div>
    </section>
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.