0

I am having issues getting the below code to work. I created an Advanced Custom Field (top_articles) with a checkbox containing three options: top_articles_left, top_middle, top_articles_right. I would it is so that when you are within a post you can check off that you want that post featured and it then shows up in the respective three zones I have designated. How could I accomplish this with ACF and have it fetch the correct post in WordPress?

    <section class="top-articles">
        <h2>Top Articles</h2>

        <?php $articles_query = new WP_Query('category_name=main&posts_per_page=1');
        while ($articles_query->have_posts()) : $articles_query->the_post(); ?>
        <?php if (get_field('top_articles') == 'top_articles_left'): ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('large'); ?>
            </a>
        </article>
        <?php endif; endwhile; ?>

        <?php $articles_query = new WP_Query('category_name=main&posts_per_page=1');
        while ($articles_query->have_posts()) : $articles_query->the_post(); ?>
        <?php if (get_field('top_articles') == 'top_articles_middle'): ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('large'); ?>
            </a>
        </article>
        <?php endif; endwhile; ?>

        <?php $articles_query = new WP_Query('category_name=main&posts_per_page=1');
        while ($articles_query->have_posts()) : $articles_query->the_post(); ?>
        <?php if (get_field('top_articles') == 'top_articles_right'): ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('large'); ?>
            </a>
        </article>
        <?php endif; endwhile; ?>

    </section>

1 Answer 1

1

You'll have to look in the database to see how ACF stores the checkbox values. As far as I know, they're not saved as flat meta values under a single key, which makes it not possible to efficiently query on that data.

The queries you have now aren't going to work, you're just loading a single post in each of them and checking if the field has a specific value. If that one post doesn't have the value, you'll see nothing. What you want to be able to do is query for posts with the value rather than querying for posts and then testing for a value.

If you were to save the data via your own meta box as post meta data, you could do a simple meta query to load the posts that have those values, see WP_Query for more info.

$args = array(
    'category_name' => 'main',
    'meta_key' => 'top_articles',
    'meta_value' => 'top_articles_left',
    'posts_per_page' => -1
);
$query = new WP_Query( $args );
1
  • 1
    @Brian - another option with ACF is to use a select dropdown menu rather than checkboxes. You'd only be able to select a single value per post, but ACF will store that value in a way that you can then query on.
    – Milo
    Jan 2, 2013 at 19:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.