0

So I have tons of products being displayed through my custom post type of products made by the 'Advanced Custom Field' and 'Custom Post Type UI' plugins. I made a very simple custom field named "stock" which is a checkbox and if true then the word "Out of Stock" is echoed next to the product, nothing to crazy very simple.

Example of how this is done:

<?php if (get_field('stock')): ?>
<div class="stock"><p>Out of Stock</p></div><!--out of stock-->
<?php endif; ?>

Now my question here is how can I have a list or display all of my products that have the field "Stock" set to true so that I can determine which ones I have set out of stock and not have to go through 1000 products to see which one I set out of stock.

So lets say out of 1000 products you have 100 out of stock. There is no way that by memory you will remember each 100 items you set out of stock. How can I see a list of the ones I set out of stock meaning the products that have <?php get_field('stock') = true ?>

Also my products are being displayed through a regular while loop, looping through all my products to be displayed in my taxonomy.php like so:

<?php while( have_posts() ) : the_post(); ?>

//looping products

<?php endwhile; ?>
1

You should be able to query ACF fields in the same way you query normal custom fields. With that in mind, you can do a simple meta_query to get a list of posts from the selected field

You can try the following:

$args = array(
    'post_type'  => 'products',
    'meta_query' => array(
        array(
            'key'     => 'stock',
            'value'   => '1',
            'compare' => 'IN',
        ),
    ),
);
$query = new WP_Query( $args );

EDIT

From comments, the correct value for the value parameter in the meta_query should be 1, not true

| improve this answer | |
  • Hey Pieter thanks for the reply. So I tried the meta_query you've posted and it doesn't work for some reason. It just shows my else statement of "there are no posts here". – Lucas Santos Jan 13 '15 at 8:32
  • Are you running this as a separate query? You will need to make sure of how the value is saved from your custom field. – Pieter Goosen Jan 13 '15 at 8:36
  • well yes this is a custom query my main query has been changed in my function.php with pre_get_posts by the code you provided last month. Other than that this is a custom query using WP_Query. – Lucas Santos Jan 13 '15 at 8:39
  • Remove the compare parameter or set it to IN and test it again. If this does not work, do a var_dump($query); and check what your query looks like. Also, make sure that key and value parameters are correct with what is set in the db. I haven't worked with ACF before, so I'm not sure how the data is stored. – Pieter Goosen Jan 13 '15 at 8:43
  • Didn't work so I var_dump my query after the $query = new WP_Query( $args ); and a lot of arrays are displaying. So much that I don't think ill be able to post it all here. It's showing TONs of arrays. Anything I can do from here? – Lucas Santos Jan 13 '15 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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