Php 使用数组中的自定义字段值查询post
对于具有良好值(12)的帖子,我的自定义字段vardump()为: 我的查询参数是:Php 使用数组中的自定义字段值查询post,php,arrays,wordpress,Php,Arrays,Wordpress,对于具有良好值(12)的帖子,我的自定义字段vardump()为: 我的查询参数是: $artistArg = array( 'numberposts' => -1, 'category_name' => 'oeuvres', 'meta_query' => array( array( 'key' => 'Custom_field', '
$artistArg = array(
'numberposts' => -1,
'category_name' => 'oeuvres',
'meta_query' => array(
array(
'key' => 'Custom_field',
'compare' => 'EXISTS',
'value' => array(12,29,34)
)
)
);
它不起作用了。。。如果我从args中删除该值,它将返回所有post,但是当我传递其中的值时,它将不返回任何内容,我尝试将其更改为“=”或“in”…我猜我在这里做错了什么…(我还尝试了“non-array”自定义字段,一切正常…)
多谢各位
编辑:
因为我现在没有找到解决方案,所以我只是这样做:
<?php
//Get the value
$authorId = get_the_ID();
//query the post with the good meta_key
$artistArg = array(
'numberposts' => -1,
'category_name' => 'oeuvres',
'meta_query' => array(
array(
'key' => 'artiste'
)
)
);
//In the query I just filter the results with the given array of the custom field...
$artDisplay = new WP_Query( $artistArg );
// The Loop
if ( $artDisplay->have_posts() ) {
echo '<h2>Art: </h2><ul id="discover">';
while ( $artDisplay->have_posts() ) {
$artDisplay->the_post();
$artiste = get_field('artiste');
if ($artiste[0] == $authorId):
echo '<li>';
echo "<div class='thumbDiscover'>";
echo "<a href='".get_permalink()."'>";
the_post_thumbnail('thumb');
echo "</a>";
echo "</div><span>";
echo "<a href='".get_permalink()."'>";
the_title();
echo "</a>";
echo "</span>";
echo '</li>';
endif;
}
echo '</ul>';
} else {
// no posts found
}
endif;
?>
所以这不是完美的…如果有人有合适的解决方案,
谢谢请尝试直接在值中传递数组
$artistArg = array(
'numberposts' => -1,
'category_name' => 'oeuvres',
'meta_query' => array(
array(
'key' => 'Custom_field',
'value' => array(12,29,34)
)
)
);
作为:
值(字符串|数组)
-自定义字段值(注意:不支持数组)
限制为“IN”、“NOT IN”、“BETWEEN”或“NOT”的比较值
(使用“EXISTS”或“NOT”时可省略此值
在WordPress 3.5及更高版本中存在的比较)
因此,您可能不使用“比较”=>“存在”
$artistArg = array(
'numberposts' => -1,
'meta_key' => 'Custom_field',
'category_name' => 'oeuvres',
'meta_query' => array(
array(
'key' => 'Custom_field',
'compare' => 'IN'
'value' => array(12,29,34)
)
)
);
在数组中传递“meta_key”=>“Custom_field”remove
,
在“value”=>数组(12,29,34)之后,
应该是“value”=>数组(12,29,34)
谢谢你,但是它什么都不做(我同意你的说法,尽管它更正确…)我没有任何php错误…如果我删除了“value”=>数组(12,29,34)总的来说,我确实用一个meta_键“Custom_field”echo$artDisplay->request得到了所有的帖子,看看sql是什么样子。比较存在的不检查值参数,你必须在中使用,还要在meta查询中添加“type”作为数值。你能确保,你把meta-key的int作为值存储,而不是数组吗它也不起作用-@Andrew我这样做了,请求看起来很正常:和(wpDB_posteta.meta_key='artiste')(我不能在这里粘贴所有的查询,有什么有趣的吗?)也没有运气…我尝试了数字和字符串(例如:数组(12,'12')或数组(12,22)…我想我明年会找到答案的,如果我发现了,我会发布答案的!!!谢谢你,但运气不好…(注:你忘了在“IN”之后昏迷了)
$artistArg = array(
'numberposts' => -1,
'meta_key' => 'Custom_field',
'category_name' => 'oeuvres',
'meta_query' => array(
array(
'key' => 'Custom_field',
'compare' => 'IN'
'value' => array(12,29,34)
)
)
);