admin管理员组文章数量:1310461
I'm trying to run a query that returns all the posts with a given value for a specific meta key. I know for a fact that this is none of the posts on my test server but it reports finding all the posts with a post count of 7 (there are 21 posts).
I have been following the notes here and the question here but I cannot get a sensible outcome.
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',// outcome is the same with and without this line
array(
'key' => 'perma',
'compare' => '=',
'value' => "{$raw_perma}"
),
),
);
// create a custom query
$my_query = new \WP_Query( $args );
echo $my_query->post_count; // 7 (regardless of $raw_perma value).
I would like to know what I am doing wrong. As "are there posts with this meta key-value pair?" seems like it should be a trivial question to answer.
Update
Found another question and tried this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'perma',
'meta_value' => "{$raw_perma}"
);
This works although I have no idea why this and not that. Would like to see an explanation in an answer.
I'm trying to run a query that returns all the posts with a given value for a specific meta key. I know for a fact that this is none of the posts on my test server but it reports finding all the posts with a post count of 7 (there are 21 posts).
I have been following the notes here and the question here but I cannot get a sensible outcome.
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',// outcome is the same with and without this line
array(
'key' => 'perma',
'compare' => '=',
'value' => "{$raw_perma}"
),
),
);
// create a custom query
$my_query = new \WP_Query( $args );
echo $my_query->post_count; // 7 (regardless of $raw_perma value).
I would like to know what I am doing wrong. As "are there posts with this meta key-value pair?" seems like it should be a trivial question to answer.
Update
Found another question and tried this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'perma',
'meta_value' => "{$raw_perma}"
);
This works although I have no idea why this and not that. Would like to see an explanation in an answer.
Share Improve this question edited Jan 19, 2021 at 17:46 Matthew Brown aka Lord Matt asked Jan 19, 2021 at 15:16 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 7- 1 Why are you using 'relation' => 'AND', for the meta_query? – Q Studio Commented Jan 19, 2021 at 16:10
- @QStudio in the hope that it might work. I have got to the point where I trying daft things because I just don't know what the issue is. – Matthew Brown aka Lord Matt Commented Jan 19, 2021 at 16:19
- 1 Perhaps go back to basics, and show the code you think should work - also show how $raw_perma is generated, and for good measures, make a SQL query directly to phpmyadmin showing the right / expected results - to ensure this is not a data issue. – Q Studio Commented Jan 19, 2021 at 16:55
- I have had a PHPMyAdmin tab open the entire time. It's not a data issue as far as I can tell. – Matthew Brown aka Lord Matt Commented Jan 19, 2021 at 17:06
- 1 Guessing, remove the AND and remove the compare - this is redundant - and read this post, might help: rudrastyh/wordpress/meta_query.html – Q Studio Commented Jan 19, 2021 at 17:36
1 Answer
Reset to default 1This works although I have no idea why this and not that. Would like to see an explanation in an answer.
Because that code sets the posts_per_page
value to -1
which means no limit and it's similar to setting nopaging
to true
, hence all found posts are returned.
it reports finding all the posts with a post count of 7 (there are 21 posts)
I think the original issue here is that you probably not aware that the property $post_count
(i.e. WP_Query::$post_count
) is actually the number of posts just for the current page, so if LIMIT
was used (in the SQL statement generated by WP_Query
), e.g. by setting the posts_per_page
value to, say, 10
which is the default value, then the $post_count
would be 10
or less depending on the total number of posts in the database which matched the specific query.
So if you want to get that total number, then use the $found_posts
property:
$my_query = new \WP_Query( array(
'posts_per_page' => 1, // just for testing
) );
echo "Number of posts with LIMIT applied: {$my_query->post_count}<br>";
echo "Number of posts without LIMIT applied: {$my_query->found_posts}";
See here if you'd like to check the other properties in WP_Query
.
Additional Notes
If you don't set the
posts_per_page
in your query args, the value will default to10
or whatever you put in the "Blog pages show at most" input on the Reading Settings admin page (wp-admin
→ Settings → Reading). And I guessed you had it set to7
?I think that
"{$raw_perma}"
could simply be written as$raw_perma
as in'meta_value' => $raw_perma
and'value' => $raw_perma
.. :)
本文标签: wp querySelecting posts with a given meta value for a meta key
版权声明:本文标题:wp query - Selecting posts with a given meta value for a meta key 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741820640a2399343.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论