admin管理员组文章数量:1323714
I'm trying to get posts that all have a custom field named webinar
, but I just get all posts. Here is what I have:
$args = array(
'showposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'key' => 'webinar',
'compare' => 'EXIST',
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while ( $loop->have_posts() ) {
$loop->the_post();
$output .= get_the_title();
}
}
I'm trying to get posts that all have a custom field named webinar
, but I just get all posts. Here is what I have:
$args = array(
'showposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'key' => 'webinar',
'compare' => 'EXIST',
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while ( $loop->have_posts() ) {
$loop->the_post();
$output .= get_the_title();
}
}
Share
Improve this question
asked Sep 10, 2020 at 15:31
MichaelMichael
2811 silver badge14 bronze badges
1 Answer
Reset to default 1That's not how meta_query
works, the documentation at WordPress states that meta_query
does not take parameters, but rather arrays of parameters
meta_query also contains one or more arrays with the following keys:
Then further down:
Important Note:
meta_query
takes an array of meta query arguments arrays (it takes an array of arrays) – you can see this in the examples below.This construct allows you to query multiple metadatas by using the relation parameter in the first (outer) array to describe the boolean relationship between the meta queries. Accepted arguments are ‘AND’, ‘OR’. The default is ‘AND’.
and:
(Note that meta_query expects nested arrays, even if you only have one query.)
So this:
'meta_query' => [
'key' => 'webinar',
'compare' => 'EXIST',
],
Should be this:
'meta_query' => [
[
'key' => 'webinar',
'compare' => 'EXIST',
]
],
However, this entire approach is wrong, and the query is going to be super slow/expensive, it does not have to be this way. Custom fields are not designed for finding posts, searching for, filtering for, querying for posts by their post meta values is super expensive/heavy/slow/unnecessary.
There are 2 alternatives that could be thousands of times faster, and easier, and they allow you to avoid having to build a custom query completely ( and they give you free pagination, REST APIs, URLs, admin screens, etc )
1. Custom Post Types, you could have created a Webinar post type. Then you'd have a webinar archive to at /webinar
which could be changed via the rewrite
parameter, archive-webinar.php
in your theme, all magically auto-created for free! There would even be a separate area of the WP Admin interface for webinars. Why go to all that effort recreating it with a custom field, save yourself the hassle. You can go to a site like https://generatewp/post-type/ or one of their competitors, and fill in some questions and it'll generate the code for you to copy paste
2: Custom Taxonomy, if you ever need to filter/search/query/restrict/etc then use a custom taxonomy, that's what they were designed for. There's a reason tags and categories aren't stored as custom fields. These give you a lot of free stuff too. Like free templates taxonomy-webinar.php
in your theme, free archive URLs, user interfaces, REST API for your javascript, etc. You can even generate the code for free with the same website as the custom post type and copy paste
Further notes:
- Don't bunch up all your output in a pointless variable, it just means your browser is sitting their waiting for output, and it makes escaping super hard. If you need it as a string for a shortcode, just use an output buffer
- Clean up after the query and call
wp_reset_postdata
just after the while loop ends, or the current post will be set to the last post of the loop rather than the current page - Avoid
'showposts' => -1,
andposts_per_page
set to-1
, set it to a super high number you know it will never reach, but never-1
, otherwise if there's an accident, if business changes, if the site adds webinars over the years, you can get memory exhaustion or timeout errors. Don't say it will never happen, instead set a high number and guarantee it can't happen
本文标签: phpWPQuery getting posts where custom field exists
版权声明:本文标题:php - WP_Query: getting posts where custom field exists 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742127318a2422000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论