0

Bottom for resolution Im going crazy I think. I cannot figure this out for the life of me. I have an advanced custom field that is a select box. The select box has multiple values and allows for multi-selections. I made 1 post and multi-selected 3 values in this drop down.

I now need to be able to show all posts that have 1 of those values. So I made a new page, and started googling trying find code to do this, and I tried many many thing, but none seem to actually work. Now, if I change this ACF to a single select, I can do it just fine. But since it is this multi select serialized array, I can't figure out the syntax.

Currently, my page with the below code is returning many post titles, including the one I want, but also including a bunch that I don't want.

keywords: Make a page to query posts with advanced custom field that is an array, query acf fields array

acf meta_key = author_link
meta_value in db = a:3:{i:0;s:12:"Triz-Journal";i:1;s:18:"Matt Campbelll III";i:2;s:8:"Garfield";}

I should be able to return that titles of only my 1 post containing 1 of those 3 values. I will never be searching more than 1 value.

Here is the code I am using at the moment, but I have also tried many others

// args
$args = array(
'numberposts'   => -1,
'post_type'     => 'post',
'meta_query'    => array(
    array(
        'key'       => 'author_link',
        'value'     => $authorname,
    )
)
);


// query
$the_query = new WP_Query( $args );
var_dump($the_query->request);
?>

<?php if( $the_query->have_posts() ): ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <p>
        <a href="<?php the_permalink(); ?>">

            <?php the_title(); ?>
        </a>
    </p>
<?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

VAR_DUMP QUERY RESULTS

string(408) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
INNER JOIN wp_postmeta   ON ( wp_posts.ID = wp_postmeta.post_id ) 
WHERE 1=1 AND 
( ( wp_postmeta.meta_key = 'author_link' AND 
CAST(wp_postmeta.meta_value AS CHAR) = 'Garfield' ) ) AND 
wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR 
wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC 
LIMIT 0, 20"

Currently, the query is returning many post titles, but not the only post that actually have the acf value of "Garfield" in it.

Can anyone help further?

RESOLUTION---------- For whatever reason, the query was pulling back multiple posts including the ones I needed. Thanks to the guys that responded, I got it figured out. Inside the query loop, I used get field to test the array of each individual post in the loop. Then, I only echo'd the title if it had a meta value matching the one I needed (which was fetched from the URL).

page-authors.php

//Get authorname from URL
<?php
if (isset($_GET['authorname'])) {
 $authorname = $_GET['authorname'];
} else {
 $authorname = 'none';
}
?>

// meta query my acf field name called author_link 
// which is a multi value array not a single text value. 
// query it for the value from the url
$args = array(
'numberposts'   => -1,
'post_type'     => 'post',
'meta_query'    => array(
    array(
        'key'       => 'author_link',
        'value'     => '"' . $authorname . '"',
        'compare'   => 'LIKE',
    )
)
);


// query
$the_query = new WP_Query( $args );
?>

// loop through the query results and test 
// them further for the meta value matching  what came from the url.
// then echo the title   if it matches
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <p>
        <a href="<?php the_permalink(); ?>">
            <?php if( in_array( $authorname, get_field('author_link') ) )
                {
                 echo the_title();
                } ?>    
        </a>
    </p>
<?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

closed as off-topic by Dave Romsey, cjbj, Howdy_McGee Sep 28 '16 at 18:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • I'd remove the meta_key line from your args as it's not needed but that shouldn't cause your current problems - perhaps do a var_dump($the_query->request); after the new and run and tweak the SQL command directly against your database to see what's happening... – bonger Jul 22 '15 at 0:50
  • Thank you for the info. I just posted the results from the var_dump after I removed the meta_key. It is still pulling a list of a lot of posts instead of the only 1 post that has the meta value Garfield. Is there a way I can query the results further and implode that array and test them further before printing the list out? – Matt Campbell Jul 22 '15 at 18:34
  • You have to do a LIKE meta query if the full value isn't identical to what you're querying for, which it won't be if the data is in a serialized string. Note though that a LIKE query can be pretty slow. Storing data as serialized strings is far from ideal if you need to query against it. – Milo Jul 22 '15 at 19:11
  • When I do use a Like query it pulls back everything, including the 1 post I want. What about, if I leave it as is, but then implode the array and test for the value 'Garfield' before I echo its title? Is there a way to pull back the meta_values of a specific post by post id? – Matt Campbell Jul 22 '15 at 19:17
  • If the query without LIKE returns many posts except the post you want, & the query with LIKE returns many posts including the post you want, then I'd guess you actually have many posts with a (non-serialized) "Garfield" value, perhaps left over from your previous experimenting ... so either just remove the bogus author_links from these, or put double quotes around the value arg ('"' . $authorname . '"'), & your original query with the LIKE should work as you expect... – bonger Jul 22 '15 at 21:28
1

I would advise using the built-in ACF functions for this. Elliot has provided a full toolbox for you and documentation for all field types:

<?php

// Conditional statement (Single Value)   
if(get_field('page_layout') == "col_1")
{
    //...
}


//Conditional statement (Multiple Values)
if( in_array( 'col_1', get_field('page_layout') ) )
{
    //...
}
?>

Check out the ACF documentation: http://www.advancedcustomfields.com/resources/select/

  • This is fine for pulling values and working with them which I know how to do, but how does this translate into a full query that returns all posts with a certain value in its array?? I am using their documentation but the query section is a little bit out of date, and currently it is what I am trying to use. I just added a query var dump. – Matt Campbell Jul 22 '15 at 18:36
  • Could I somehow get the Post ID and query it inside a loop to build the list? if( in_array( 'Garfield', get_field('author_link') ) ) { for each Get its post ID query it by post id echo title result } – Matt Campbell Jul 22 '15 at 18:51
  • What is the slug of your custom field? Assuming you created a Field Group in ACF and then individual fields inside of that, is "author_link" the Field Name of the custom field you want to filter posts with? – jdm2112 Jul 22 '15 at 22:13

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