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 yourwhile
statement you use:
andendwhile
. 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();
}