Reputation: 165
This should be simple, but I just can't pinned down a good example of the correct syntax to do this.
I want to filter my posts by the meta_queries but order them by the specified meta_key.
When I run the code as is it results an infinite loop. I only included the problem code the other code is your basic Loop code that the query_post runs in.
Also all the PHP variables are correct and are not the problem.
$args2 = array(
'meta_key' => '_count-views_all',
//'meta_value' => $id,
'orderby' => 'meta_value_num',
'order' => $sortOrder,
'posts_per_page' => 9,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'contributorid1',
'value' => $id,
'compare' => '='
),
array(
'key' => 'contributorid2',
'value' => $id,
'compare' => '='
)
)
);
$posts = query_posts($args2);
}
Here is another query that works completely without issue to cross reference. The two run on the same page but the are nested in an if else statement
$args1 = array(
//'meta_key' => 'contributorid1',
//'meta_value' => $id,
'order' => $sortOrder,
'orderby' => 'title',
'posts_per_page' => 9,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'contributorid1',
'value' => $id,
'compare' => '='
),
array(
'key' => 'contributorid2',
'value' => $id,
'compare' => '='
)
)
);
$posts = query_posts($args1);
Upvotes: 1
Views: 1928
Reputation: 11
The query looks reasonable to me. The only method by which I see this running into an infinite loop is if this query runs within the post loop. When you use query_posts as you are, it will change the state of the global $wp_query, which is used for the pointer in the main posts loop.
If it kept hitting query_posts within the loop, it would continually change the state of the global $wp_query object, and reset the pointer for the current post to the first post of that new query, which would ultimately create the infinite loop.
If this code is being used within the loop, I'd recommend instead using something like
$query = new WP_Query($args2);
if ($query->have_posts()) { ... etc; }
If you need to then set up global post data within it, be sure to use wp_setup_postdata
or $query->the_post()
and wp_reset_postdata
or wp_reset_query
appropriately when you are finished using that post as the global post information.
Upvotes: 1