Skip to main content
2 of 3
added 468 characters in body
SixfootJames
  • 437
  • 3
  • 11
  • 23

Querying Advanced Custom Fields

I've been back and forth with this and not able to get my Advanced Custom Field query working.

According to the documentation in the link above, this is what my array should look like:

$args = array(
    'numberposts' => -1,
    'post_type' => 'event',
    'meta_key' => 'location',
    'meta_value' => 'Melbourne'
);

Below is my array. The problem is clearly in the meta_key and meta_value I am trying to query. Without the meta_key and meta_value, all the Products are being returned as expected. I have been into the database to make sure I have the right values so this looks correct to me. Is there something that stand out to anyone I might be missing? Thanks

        <section class="entry-content clearfix search-results sectiondrop" itemprop="articleBody">
            <h3>Search Results</h3>
            <?php 
                $args = array( 
                    'post_type' => 'Product', 
                    'posts_per_page' => -1, 
                    'orderby' => 'title', 
                    'order' => 'ASC', 
                    'meta_key' => 'product_type',
                    'meta_value' => 'Paper'
                );
                $loop = new WP_Query( $args ); ?>
                <ul>
                <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                    
                        <li>
                            <strong><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> - <?php the_field('product_type'); ?></strong><br>
                            <?php the_field('product_description'); ?><br><br>
                        </li>
                    
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
                </ul>
            <?php } ?>
        </section>

EDIT:

This is what my array looks like now:

$args = array(
    'post_type' => 'Product', 
    'posts_per_page' => -1, 
    'orderby' => 'title', 
    'order' => 'ASC',
    'meta_query' => array(
           array(
               'key' => 'product_type'
           )
        )

);

Just the "key" alone does the trick right, but as soon as I add the "value" the search does not return any results. And I know that the value should be "Paper" in this case.

SixfootJames
  • 437
  • 3
  • 11
  • 23