0

I'm using a wp_query to find custom posts that have a specified numeric value inside of a custom field. That custom field's value is an array of numbers (IDs of selected posts using Advanced Custom Field's Relationship field).

So I am trying to find all posts that have the specified value inside of that field's array.

Here's my code:

foreach ($selectedAuthors as $myAuthor){
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'resource',
        'resource_types' => 'ml-special-reports',
        'meta_query' => array (
            'key' => 'qd_resource_author_selector',
            'value' => $myAuthor,
        ),
        'orderby' => 'title',
        'order' => 'ASC'
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) { 
        echo '<h3>'.get_the_title($myAuthor).'</h3>';
        while ( $query->have_posts() ) :
            $query->the_post();
            echo '<h3>'.get_the_title().'</h3>';
        endwhile;
        wp_reset_postdata();
    }
}

If I var_dump() $selectedAuthors I get:

array(3) { [0]=> int(214) [1]=> int(216) [2]=> int(211) }

Each run through the while($query->have_posts()) returns every post of the type resource with the resource_types taxonomy term ml-special-reports regardless of whether or not $myAuthor exists in the qd_resource_author_selector custom field's array.

Any thoughts on what I'm doing wrong?

0

First of all, your meta_query is wrong. It should be an array of an array, not just an array

So, the following

'meta_query' => array (
    'key' => 'qd_resource_author_selector',
    'value' => $myAuthor,
),

becomes

'meta_query' => array (
    array(
        'key' => 'qd_resource_author_selector',
        'value' => $myAuthor,
    ),
),

Secondly, you can optimize your query. You are running a query for every value. If you have 100 values, you are going to run 100 queries which is expensive

You can optimize your query by adding your values in an array and passing the array directly to your meta_query

Just a few other concerns

  • Run a proper tax_query. The syntax you are using is depreciated

  • Your first instance of get_the_title() looks out of place outside the loop. Not sure what you are doing there

  • Do not mix your syntax, it is confusing and really hard to debug on failure. For your if statement you are using curlies (which I prefer as basically all editors support them, very easy to debug) and in your while statement you use : and endwhile. I would recommend that you use curlies and stick with them in future

With all said, you can try something like this without a foreach loop

$myAuthor = array('value1', 'value2', 'value3');
$args = array(
    'posts_per_page' => -1,
    'post_type' => 'resource',
    'tax_query' => array(
        array(
            'taxonomy' => 'resource_types',
            'field'    => 'slug',
            'terms'    => 'ml-special-reports',
        ),
    ),
    'meta_query' => array (
        array(
            'key'       => 'qd_resource_author_selector',
            'value'     => $myAuthor,
            'compare'   => 'IN',
        ),
    ),
    'orderby' => 'title',
    'order' => 'ASC'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { 
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<h3>'.get_the_title().'</h3>';
    }
    wp_reset_postdata();
}
0

I solved it using some of Pieter Goosen's code, and taking a closer look at the field's value in SQL. It was saving the information in a strange manner that required me to wrap the value in quotation marks to return the correct posts.

Here's the working code:

foreach ($selectedAuthors as $myAuthor){
    echo '<hr /><p>my author = '.$myAuthor.'</p>';  
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'resource',
        'tax_query' => array(
            array(
                'taxonomy' => 'resource_types',
                'field'    => 'slug',
                'terms'    => 'ml-special-reports',
            ),
        ),
        'meta_query' => array (
            array(
                'key' => 'qd_resource_author_selector',
                'value' => '"'.$myAuthor.'"',
                'compare' => 'LIKE',                       
            ),
        ),
        'orderby' => 'title',
        'order' => 'ASC'
        );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) { 
        echo '<h3>'.get_the_title($myAuthor).'</h3>';
        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<h3>'.get_the_title().'</h3>';
        }
        wp_reset_postdata();
    }
}

So on this line I had to force quotation marks to appear:

'value' => '"'.$myAuthor.'"',

Now it returns the Author's name, and then the name of each post with the correct taxonomy term that they are assigned to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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