0

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.

EDIT

I've looked again at the DB, the product from wp_posts through to wp_postmeta and filtered meta_value by "Paper" and they all return the following a:1:{i:0;s:9:"Paper";} where I supposedly should have just "Paper"

1 Answer 1

1

Try using a meta_query in your $args. You are far more flexible this way. For the full reference of the possibilities check the Codex

$args = array( 
    'post_type' => 'Product', 
    'posts_per_page' => -1, 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_query' => array(
       array(
           'key' => 'product_type',
           'value' => 'Paper',
           'compare' => '=',
       )
    )
);
7
  • Thanks Fischi. I had read through the Codex and tried this option. Let me give it another spin and see what comes of it. Commented Jan 29, 2014 at 11:02
  • I've tried your solution Fischi and it didn't work. I'm wondering if I am using the right name and value? It's got to be this but looking at the DB, some of the meta_key's for ACF starts with an underscore...could that be the problem? Commented Jan 29, 2014 at 11:41
  • Yeah, that could be the problem. You have to enter the exact key as it is in your database.
    – fischi
    Commented Jan 29, 2014 at 11:44
  • nope, the underscores aren't the problem. AFAIK, ACF names starting with an underscore refer to the unique id of that field.
    – RRikesh
    Commented Jan 29, 2014 at 13:29
  • This is when I take a look at the generated SQL query to see what is going on. var_dump( $loop->request ); Commented Jan 29, 2014 at 19:45

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.