如果变量是,我需要在wp\\u查询中添加更多参数!通过快捷码时为空。我正在传递循环中要使用的几个自定义字段名的名称。
我希望它像这样工作:如果custom\\u field\\u 1是!空,但custom\\u field\\u 2为空,请将第一个数组添加到$args数组。如果custom\\u field\\u 1为!空且custom\\u field\\u 2为!空,将第二个数组添加到$args数组。
我觉得我在大致范围内,但它工作不正常。如果我手动将每个meta\\u查询添加到$args数组,它将返回正确的数据。
任何帮助都将不胜感激。
// Attributes
extract( shortcode_atts(
array(
\'pagination\' => \'true\',
\'query\' => \'\',
\'category\' => \'\',
\'tag_name\' => \'\',
\'custom_field_1\' => \'\',
\'custom_field_2\' => \'\',
\'relation_operator\' => \'\',
\'number_posts\' => \'\',
\'order_by\' => \'\',
\'order\' => \'\',
), $atts ));
$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
$custom_field_query = array ();
// if $custom_field_1 is not empty and $custom_field_2 is empty
if (!empty($custom_field_1) && empty($custom_field_2) ) {
array_push($custom_field_query, array (
\'meta_query\' => array(
array(
\'key\' => $custom_field_1,
\'value\' => \'\',
\'compare\' => \'!=\'
) ) ) );
}
// if $custom_field_1 is not empty and $custom_field_2 is not empty
if (!empty($custom_field_1) && !empty($custom_field_2) ) {
array_push($custom_field_query, array (
\'meta_query\' => array(
\'relation\' => $relation_operator,
array(
\'key\' => $custom_field_1,
\'value\' => \'\',
\'compare\' => \'!=\',
),
array(
\'key\' => $custom_field_2,
\'value\' => \'\',
\'compare\' => \'!=\',
) ) ) );
}
// if $custom_field_query has array custom_field_query gets added to $args
if(!empty($custom_field_query)){
$args[\'custom_field_query\'] = $custom_field_query;
}
// WP_Query arguments
$args = array(
\'paged\' => $paged,
\'post_type\' => array( \'page\', \' post\' ),
\'post_status\' => array( \'publish\' ),
\'category_name\' => $category,
\'tag\' => $tag_name,
\'category__in\' => $theCatId,
\'posts_per_page\' => $number_posts,
\'order\' => $order,
\'orderby\' => $order_by,
);
// The Query
global $wp_query,$paged,$post;
$my_query = new WP_Query( $args );