Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Only Display posts with specific custom field value within string or array

Ok so I have everything displaying correctly but if there are terms other than the conditional term "Non-Marine" within the field('value') for the custom field "item_tags" it does not display those.

Basically I'm looking for posts with: 1.Custom post type of - ait-dir-item 2.Custom field location - annapolis 3.Custom field item_tags - Non-Marine (this value is within other terms seperated by commas)

I'm also not sure if these "values" are strings or arrays?

Here is the code I have so far:

<?php
$args = array( 'post_type' => 'ait-dir-item', 
               'meta_query' => array(
                    array(
                        'key' => 'location',
                        'value' => 'annapolis'
                    ),
                    array(
                        'key' => 'item_tags',
                        'value' => 'non-marine'
                    )
                ),
                'orderby' => 'title', 
                'order' => 'ASC',
               'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title('<h3 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h3>');
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile;
?>

Thanks for any input!

Cory-

2 Answers

Hey Zac, Thanks for the reply.. I don't thing you need to say meta when you are in the meta_query.

I solved my problem by adding compare => 'LIKE', after 'value' => 'non-marine'.

Thanks! Cory-

Zac Gordon
Zac Gordon
Treehouse Guest Teacher

Hmm, interesting. Glad you got it working!

That's was gonna be one of my next suggestions ;p

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Check this documentation here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

I believe you need to say meta_value and meta_key:

$query = new WP_Query( 
     array( 
        'meta_key' => 'color', 
        'meta_value' => 'blue' ) 
);