Wordpress query_post array with Advanced Custom Fields (probably a simple solution)

Gregory Edgerton picture Gregory Edgerton · Dec 5, 2012 · Viewed 9.7k times · Source

Working on a Wordpress site and was wondering if anyone could point me in the right direction. I have the following query_post which filters posts on my template and its working great.

query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, ) );

How would I append this to include a check for a specific value from Advanced Custom Fields? Posts in this category have a radio button with three options 'primary' 'secondary' 'standard' I want to be able to check against each value i.e if you belong in 'galoretv' and 'standard' do this.

I have the page executing and sorting with the parameters above, just not sure how to add the ACF value. I was able to get it to work using the sticky option but thats gonna work cause i need to have primary and secondar optionality. This is show i had it working with the sticky.

query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'post__in'=>get_option('sticky_posts')) );

The radio buttons field is called 'landing-grid-placement'

Any thoughts? Been checking the documentation, can't figure it out. http://www.advancedcustomfields.com/docs/field-types/checkbox/

Thought this would work but it didn't

query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'landing-grid-placement' => 'primary') );

Any help would be greatly appreciated. This is probably a simple syntax issue but its eluding me and causing me a lot of issues. Been searching for an answer but haven't got it right yet. Thank you advance to whoever reads this and a double thank you to anyone who contributes a solution.

APPENDED CODE PER NOTE BELOW

 <?php

    $args = array(
        'post_type' => 'post',
        'category-slug' => 'models-galore',            
        'showposts'=>1, 
        'meta_query' => array(
            array(
                'key' => 'grid_location',
                'value' => 'primary',
                'compare' => '=', 
                'type' => 'CHAR' 
            )
        )
    );

    $query = new WP_Query($args);
    if($query->have_posts()) {
        while($query->have_posts()) {
            $query->the_post();
            ?>

            <li class="span8">
                <div class="thumbnail">
                    <a href="<?php echo get_permalink(); ?>"><?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?></a>
                    <h3>
                        <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
                    </h3>
                </div>
            </li>
            <?php
        }
    }

    ?>

Answer

Philippe Boissonneault picture Philippe Boissonneault · Dec 5, 2012

You can do this with a meta_query

Here's the doc

And here's an example:

$args = array(
    //...
    'post_type' => 'post', 
    'paged'=>$paged, 
    //...
    'meta_query' => array(
        array(
            'key' => 'landing-grid-placement',
            'value' => 'primary',
            'compare' => '=', //default
            'type' => 'CHAR' //default
        )
    )
);

//This is the shoposts option (deprecated since 2.1, now use posts_per_page)
$args['posts_per_page'] = -1; //-1 = infinite

//to add taxonomy
$args['tax_query'] = array(
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => 'galoretv'
    )
);

$query = new WP_Query($args);
if($query->have_posts()) {
    while($query->have_posts()) {
        $query->the_post();
        ?>
        <li class="span8">
            <div class="thumbnail">
                <a href="<?php echo get_permalink(); ?>"><?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?></a>
                <h2>
                    <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
                </h2>
            </div>
        </li>
        <?php
    }
}

Maybe there is a simpler solution for you :

$args = array(
    //...
    'post_type' => 'post', 
    'paged'=>$paged, 
    //...
    'meta_key' => 'landing-grid-placement', 
    'meta_value' => 'primary',
    'meta_compare' => '=' //default
    )
);
$query = new WP_Query($args);
if($query->have_posts()) {
    while($query->have_posts()) {
        $query->the_post();
        //do what you would normally do in your loop
    }
}