通过元值
-
-
请注意[在发布问题之前,您应该已经研究了该问题并尝试解决该问题.](http://wordpress.stackexchange.com/questions/how-to-ask)如果您不是品牌商在这里,我可能会否决这个问题,然后继续前进,而不是回答它.本着" Welcometothe Stack"的精神,这是您的自由蜂.请查看[问]以后的问题.Please be aware that [you are expected to have researched the problem and made an attempt at solving it before posting a question.](http://wordpress.stackexchange.com/questions/how-to-ask) Had you not been brand new here I would have probably down-voted the question and moved on, rather than answer it. In the spirit of "Welcome to the Stack" this is your free-bee. Please take a look at [ask] for future questions.
- 8
- 2014-05-11
- s_ha_dum
-
由于以下未接受的回答,这使我浪费了一些时间.所以我要把我的两分钱留在这里.他从未回答,也没有接受下面的答案.在这里周围有许多类似的问题时,为什么不删除这些问题呢?This one just made me lose some time because of the not accepted answer below. So I'm leaving here my 2 cents. He never answered, nor accepted the answer below. Why don't you just remove this questions while there are dozens of similar questions around here?
- 0
- 2019-07-21
- mircobabini
-
4 个回答
- 投票数
-
- 2014-05-11
您要的是
meta_query
$args = array( 'meta_query' => array( array( 'key' => 'cp_annonceur', 'value' => 'professionnel', 'compare' => '=', ) ) ); $query = new WP_Query($args);
What you are asking for is a
meta_query
$args = array( 'meta_query' => array( array( 'key' => 'cp_annonceur', 'value' => 'professionnel', 'compare' => '=', ) ) ); $query = new WP_Query($args);
-
@Beginner:如果此方法解决了问题,请将其标记为"已接受".在左侧的投票箭头附近寻找对勾标记.@Beginner : if this solved the problem please mark it "Accepted". Look for the check mark near the vote arrows on the left.
- 3
- 2014-05-11
- s_ha_dum
-
由于某种原因,使用`new WP_Query($ args)`然后调用`get_posts`对我不起作用.但是,直接将$ args数组作为参数传递给get_posts函数.For some reason using `new WP_Query($args)` and then calling `get_posts` doesn't work for me. Directly passing the `$args` array to the `get_posts` function as an argument works however.
- 0
- 2020-05-05
- Kunal
-
- 2014-05-11
有两种方法可以做到:
-
在
pre_get_posts
上拦截主要查询:add_action( 'pre_get_posts', function( $query ) { // only handle the main query if ( ! $query->is_main_query() ) return; $query->set( 'meta_key', 'cp_annonceur' ); $query->set( 'meta_value', 'professionnel' ); } );
-
添加其他查询
$second_loop = get_posts( array( 'meta_key' => 'cp_annonceur', 'meta_value' => 'professionnel', ) );
在此答案中可以找到更多示例.
There are two ways to do that:
Intercept the main query on
pre_get_posts
:add_action( 'pre_get_posts', function( $query ) { // only handle the main query if ( ! $query->is_main_query() ) return; $query->set( 'meta_key', 'cp_annonceur' ); $query->set( 'meta_value', 'professionnel' ); } );
Add an additional query
$second_loop = get_posts( array( 'meta_key' => 'cp_annonceur', 'meta_value' => 'professionnel', ) );
A more throughout example can be found in this answer.
-
很高兴知道get_posts()的简短方法Nice to know the short way with get_posts()
- 2
- 2017-01-26
- Andrew Welch
-
如何使用多个元键/值来实现此目的?How do you do this with multiple meta ket/values ?
- 0
- 2020-04-09
- G-J
-
@ G-J,您可能想看看[此示例](https://wordpress.stackexchange.com/a/105705/385).@G-J you might want to take a look at [this example](https://wordpress.stackexchange.com/a/105705/385).
- 0
- 2020-04-15
- kaiser
-
- 2016-01-28
我使用了自定义选择(可能会有更好的性能)
$posts = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'cp_annonceur' AND meta_value = 'professionnel' LIMIT 1", ARRAY_A);
I used custom select (might be better performance)
$posts = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'cp_annonceur' AND meta_value = 'professionnel' LIMIT 1", ARRAY_A);
Inspired from https://tommcfarlin.com/get-post-id-by-meta-value/
-
它可能具有更好的性能,但是却放弃了使用Wordpress函数搜索(和缓存)数据的整个想法.而且,如果WP决定更改表结构,将会发生什么?:)It might have better performances, but it throws away the whole idea of having Wordpress functions to search (and cache) data. And, also, what will happen if WP decides to change the table structure? :)
- 3
- 2017-03-03
- Erenor Paz
-
@ErenorPaz我明白您在说什么,但是如果您有多个元键/值作为条件,这种方式将使操作变得容易.是否有处理多个条件的官方方法?@ErenorPaz I understand what you are saying but this way would make it easy if you had multiple meta key/values as a criteria... Is there an official way to handle multiple criteria?
- 0
- 2020-04-09
- G-J
-
您的意思是类似" WHEREmetatable1.meta_key='cp_annonceur'ANDmetatable1.meta_value='professionnel'ANDmetatable2.meta_key='cp_other_meta'ANDmetatable2.meta_value='other_value'`的东西吗?(请注意,我想我们使用两个名称"metatable1"和"metatable2"在同一posts_meta表上进行联接).这可以通过在查询中添加字段"meta_query"(作为数组)来实现.看一下:https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters并转到带有多个"meta_key"的" orderby"段落You mean something like `WHERE metatable1.meta_key = 'cp_annonceur' AND metatable1.meta_value = 'professionnel' AND metatable2.meta_key = 'cp_other_meta' AND metatable2.meta_value = 'other_value'`? (Note that i suppose we do a join on the same posts_meta table using two names `metatable1` and `metatable2`). This can be achieved adding field `meta_query` (as an array) to the query. Take a look at: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters and go to paragraph "‘orderby’ with multiple ‘meta_key’s"
- 0
- 2020-04-10
- Erenor Paz
-
- 2017-10-04
我们可以通过WordPress的元查询获得所需的结果:
// the meta_key 'diplay_on_homepage' with the meta_value 'true' $cc_args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'meta_key' => 'cp_annonceur', 'meta_value' => 'professionnel' ); $cc_query = new WP_Query( $cc_args );
有关元查询的更多详细指南,请访问此博客: http://www.codecanal.com/get-posts-meta-values/
We can get the desired result with Meta query of the WordPress :
// the meta_key 'diplay_on_homepage' with the meta_value 'true' $cc_args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'meta_key' => 'cp_annonceur', 'meta_value' => 'professionnel' ); $cc_query = new WP_Query( $cc_args );
For more detailed guide regarding meta query follow this blog : http://www.codecanal.com/get-posts-meta-values/
我想列出所有键为
cp_annonceur
且值为professionnel
的帖子.