简体   繁体   中英

No posts found within the Wordpress loop based on custom arguments

I've set one simply parameter in my args array but not getting any of the appropriate posts through.

With use of Advanced Custom Fields, I created a 'Select' option in the 'post' types, and the option being 'Featured: Yes'. About 4 are set as featured but still states no posts are found.

** I've provided a screenshot of the page. As you'll see, there are posts on the bottom half of the page which are coming through using the standard loop, but I've setup a new loop to display just Featured posts at the top. Maybe I am meant to end the global loop first?

Here is my current setup:

<?php 

// args
$args = array(
    'numberposts' => -1,
    'meta_key' => 'feature_post',
    'meta_value' => 'Yes'
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

<ul class="bxslider">

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
        <div class="featured-article">
            <div class="category-label">Health</div>
            <i class="category-label-end"></i>
            <?php echo the_post_thumbnail(); ?>
            <div class="featured-article-title">
                <h2><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h2>
            </div>
        </div>
    </li>

    <?php endwhile; ?>
</ul>

<?php else : echo '<p style="color:#fff;">no posts</p>'; ?>

<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Advanced Custom Fields

在此处输入图片说明

A post that has been set to Featured.

在此处输入图片说明

Post Page:

在此处输入图片说明

I suspect it's because you can have several checkboxes in the one field, meaning ACF needs to store the value as an array, rather than a single string.

I just did a test, and this is the meta_value I get based on your setup:

a:1:{i:0;s:3:"Yes";}

which won't match the literal Yes you're using.

In this particular case, I'd try using ACF's True / False field type. If true, it stores 1 in the meta_value field, which would work with the approach you're using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM