将查询字符串中的数组添加到 WP meta_query
Adding an array from a query string to a WP meta_query
我正在从查询字符串中收集值作为数组。然后收集这些值并将它们放入 'meta_query'.
的值中
我遇到了问题,WP_Query 没有任何输出。我觉得 'meta_query' 中的第二个数组有问题。
我已尝试将比较更改为 'LIKE',这会显示所有内容而不是查询中的内容。
查询字符串:
?variable[]=value1&variable[]=value2
PHP代码:
<?php
$variable_selected = $_GET['variable'];
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'my_post_field',
'value' => $variable_selected,
'compare' => 'IN'
)
)
);
$posts = new WP_Query( $filter );
这方面的任何帮助都会很棒。
我会先在 $variable_selected 上 运行 一个 var_dump 以确保您获得了所需的数组。此外,由于您只使用一个元 key/value 字段,您可能不需要在查询中使用 OR,因此您可以执行如下操作。希望有所帮助。
<?php
$variable_selected = $_GET['variable'];
// Use the var_dump to test to see if array is being pulled in properly.
var_dump ( $variable_selected );
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
array(
'key' => 'my_post_field',
'value' => $variable_selected,
'compare' => 'IN'
)
)
);
$posts = new WP_Query( $filter );
这使得 meta_query 动态取决于查询字符串中有多少个值。
$meta_query = array();
if ( ! empty( $_GET["variable"] ) ) {
if ( is_array( $_GET["variable"] ) ) {
$meta_query['relation'] = 'OR';
foreach ( $_GET["variable"] as $value ) {
$meta_query[] = array(
'key' => 'my_post_field',
'value' => sanitize_text_field( (string) $value ),
'compare' => 'LIKE'
);
}
} else {
$meta_query = array(
'key' => 'my_post_field',
'value' => sanitize_text_field( (string) $_GET["variable"] ),
'compare' => '='
);
}
}
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
'relation' => 'OR',
$meta_query
)
);
$posts = new WP_Query( $filter );
我正在从查询字符串中收集值作为数组。然后收集这些值并将它们放入 'meta_query'.
的值中我遇到了问题,WP_Query 没有任何输出。我觉得 'meta_query' 中的第二个数组有问题。
我已尝试将比较更改为 'LIKE',这会显示所有内容而不是查询中的内容。
查询字符串:
?variable[]=value1&variable[]=value2
PHP代码:
<?php
$variable_selected = $_GET['variable'];
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'my_post_field',
'value' => $variable_selected,
'compare' => 'IN'
)
)
);
$posts = new WP_Query( $filter );
这方面的任何帮助都会很棒。
我会先在 $variable_selected 上 运行 一个 var_dump 以确保您获得了所需的数组。此外,由于您只使用一个元 key/value 字段,您可能不需要在查询中使用 OR,因此您可以执行如下操作。希望有所帮助。
<?php
$variable_selected = $_GET['variable'];
// Use the var_dump to test to see if array is being pulled in properly.
var_dump ( $variable_selected );
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
array(
'key' => 'my_post_field',
'value' => $variable_selected,
'compare' => 'IN'
)
)
);
$posts = new WP_Query( $filter );
这使得 meta_query 动态取决于查询字符串中有多少个值。
$meta_query = array();
if ( ! empty( $_GET["variable"] ) ) {
if ( is_array( $_GET["variable"] ) ) {
$meta_query['relation'] = 'OR';
foreach ( $_GET["variable"] as $value ) {
$meta_query[] = array(
'key' => 'my_post_field',
'value' => sanitize_text_field( (string) $value ),
'compare' => 'LIKE'
);
}
} else {
$meta_query = array(
'key' => 'my_post_field',
'value' => sanitize_text_field( (string) $_GET["variable"] ),
'compare' => '='
);
}
}
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
'relation' => 'OR',
$meta_query
)
);
$posts = new WP_Query( $filter );