我建议不要让逻辑过于复杂化。你的问题的一般思维过程是正确的。假设当您将meta\\u查询逻辑直接添加到查询并运行它时,以上所有内容都可以正常工作,那么您的代码应该尝试遵循相同的思维模式。
您正在访问全局列表,因此请先列出这些列表。这也将使它们在以后您需要回到您所做的事情时易于参考:
// Globals.
global $wp_query, $paged, $post;
现在您获得了简短的代码属性,不鼓励在这里使用extract,因此我建议将其设置为变量:
Note: 您将需要更新引用根据快捷码属性数组提取的变量的位置。在这个例子中,我刚刚将其命名为$sc
// Shortcode attributes.
$sc = shortcode_atts(
array(
\'pagination\' => \'true\',
\'query\' => \'\',
\'category\' => \'\',
\'tag_name\' => \'\',
\'custom_field_1\' => \'\',
\'custom_field_2\' => \'\',
\'relation_operator\' => \'\',
\'number_posts\' => \'\',
\'order_by\' => \'\',
\'order\' => \'\',
),
$atts
);
接下来,如果满足条件,您希望修改您的WP\\u查询参数,因此请将您的默认查询参数放在后面,$args:
Note: get_query_var 接受一个默认值作为传递的第二个参数,因此可以去掉该条件并使用默认参数集转换为int(这比问题中使用的逻辑快得多,并产生相同的结果[即,与转换为int相比,absint在内部调用intval])。我可能会直接把它放在$args中,而不是为它设置一个变量
// WP_Query arguments
$args = array(
\'paged\' => ( int ) get_query_var( \'paged\', 1 ),
\'post_type\' => array( \'page\', \' post\' ),
\'post_status\' => array( \'publish\' ),
\'category_name\' => $sc[\'category\'],
\'tag\' => $sc[\'tag_name\'],
\'category__in\' => $theCatId,
\'posts_per_page\' => $sc[\'number_posts\'],
\'order\' => $sc[\'order\'],
\'orderby\' => $sc[\'order_by\'],
);
现在,如果满足某些条件,您需要添加条件并修改参数:
Note: 如果愿意,可以使用if(!empty($custom\\u field\\u 1))。我只是觉得($custom\\u field\\u 1)读起来更容易/写起来更快,但这只是个人喜好
// custom_field_1 is populated.
if ( $sc[\'custom_field_1\'] ) {
// custom_field_2 is populated.
if ( $sc[\'custom_field_2\'] ) {
$args[\'meta_query\'] = array(
\'relation\' => $sc[\'relation_operator\'],
array(
\'key\' => $sc[\'custom_field_1\'],
\'value\' => \'\',
\'compare\' => \'!=\',
),
array(
\'key\' => $sc[\'custom_field_2\'],
\'value\' => \'\',
\'compare\' => \'!=\',
),
)
);
// custom_field_2 is not populated.
} else {
$args[\'meta_query\'] = array(
array(
\'key\' => $sc[\'custom_field_1\'],
\'value\' => \'\',
\'compare\' => \'!=\',
),
);
}
}
现在,您已经根据需要修改了参数,因此请创建查询:
// Custom query $my_query.
$my_query = new WP_Query( $args );
我没有看到任何对$theCatId的引用,所以我假设您在脚本的其他地方处理这个问题。也可以比我在示例代码中所做的更有效地编写内容,但不管怎样,这都应该有助于为您指明获得所需结果的正确方向。
以上所有因素加在一起:
// Globals.
global $wp_query, $paged, $post;
// Shortcode attributes.
$sc = shortcode_atts(
array(
\'pagination\' => \'true\',
\'query\' => \'\',
\'category\' => \'\',
\'tag_name\' => \'\',
\'custom_field_1\' => \'\',
\'custom_field_2\' => \'\',
\'relation_operator\' => \'\',
\'number_posts\' => \'\',
\'order_by\' => \'\',
\'order\' => \'\',
),
$atts
);
// WP_Query arguments
$args = array(
\'paged\' => ( int ) get_query_var( \'paged\', 1 ),
\'post_type\' => array( \'page\', \' post\' ),
\'post_status\' => array( \'publish\' ),
\'category_name\' => $sc[\'category\'],
\'tag\' => $sc[\'tag_name\'],
\'category__in\' => $theCatId,
\'posts_per_page\' => $sc[\'number_posts\'],
\'order\' => $sc[\'order\'],
\'orderby\' => $sc[\'order_by\'],
);
// custom_field_1 is populated.
if ( $sc[\'custom_field_1\'] ) {
// custom_field_2 is populated.
if ( $sc[\'custom_field_2\'] ) {
$args[\'meta_query\'] = array(
\'relation\' => $sc[\'relation_operator\'],
array(
\'key\' => $sc[\'custom_field_1\'],
\'value\' => \'\',
\'compare\' => \'!=\',
),
array(
\'key\' => $sc[\'custom_field_2\'],
\'value\' => \'\',
\'compare\' => \'!=\',
),
)
);
// custom_field_2 is not populated.
} else {
$args[\'meta_query\'] = array(
array(
\'key\' => $sc[\'custom_field_1\'],
\'value\' => \'\',
\'compare\' => \'!=\',
),
);
}
}
// Custom query $my_query.
$my_query = new WP_Query( $args );