[英]How to exclude certain posts from showing up in wp_query if the post data does not fit parameters?
I am fairly new to PHP so this might be a simple fix. 我对PHP相当陌生,因此这可能是一个简单的修复。 I am editing a plugin and trying to modify it to where, based on a set of inputs, I want to output only certain posts whose metadata fits the parameters of the inputs and not show posts whose data does not fit. 我正在编辑一个插件,并尝试将其修改为基于一组输入的位置,我只想输出其元数据适合输入参数的某些帖子,而不显示其数据不适合的帖子。 How would I exclude posts whose data does not fit in the output? 如何排除数据不适合输出的帖子?
I have already done everything up to this point but am stuck at where if the inputted data does not fit in the post meta data it does not display. 到目前为止,我已经做完了所有事情,但是被困在哪里,如果输入的数据不适合后元数据,它将不会显示。 I am guessing I would need to use a $_post == null
function and have tried many variations of that, but nothing has worked. 我猜想我将需要使用$_post == null
函数,并尝试了许多变体,但是没有任何效果。
The is the line of code I am stuck on: 这是我坚持的代码行:
<?php if ( $creditscore >= esc_attr($min_credit_score) ) { echo "good";} else { $_Post == null; ;
} ?>
Here is the complete code: 这是完整的代码:
<?php htmlspecialchars($_GET["crd"]);
?>
<?php
$args = array(
'post_type' => 'lenders',
'posts_per_page' => $number1,
'lender_cat' => 'personal-loans',
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
$advertised_title = get_post_meta(get_the_ID(),'_cmb_advertised_title', true);
$advertised_number = get_post_meta(get_the_ID(),'_cmb_advertised_number', true);
$comparison_title = get_post_meta(get_the_ID(),'_cmb_comparison_title', true);
$comparison_number = get_post_meta(get_the_ID(),'_cmb_comparison_number', true);
$min_credit_score = get_post_meta(get_the_ID(),'_cmb_min_credit_score', true);
$btn_text = get_post_meta(get_the_ID(),'_cmb_btn_text', true);
$btn_link = get_post_meta(get_the_ID(),'_cmb_btn_link', true);
$except = get_post_meta(get_the_ID(),'_cmb_except', true);
$creditscore = htmlspecialchars($_GET["crd"], true);
?>
<?php if ( $creditscore >= esc_attr($min_credit_score) ) { echo "goodtest";} else { wp_query == null; ;
} ?>
You can add the $creditscore
value to your WP_Query
. 您可以将$creditscore
值添加到WP_Query
。 Check the documentation for Custom Field (post meta) Parameters 检查文档中的“ 自定义字段”(后元)参数
ie 即
$creditscore = htmlspecialchars($_GET["crd"], true);
$args = [
'post_type' => 'lenders',
'posts_per_page' => $number1,
'category_name' => 'personal-loans',
'meta_query' => [
[
'key' => '_cmb_min_credit_score',
'value' => $creditscore,
'compare' => '<=',
],
],
];
$wp_query = new WP_Query($args);
Notes : Since you mentioned you're new to PHP I'll add some notes. 注释 :既然您提到您是PHP的新手,我将添加一些注释。
$var = []
for arrays. 根据您使用的PHP版本,可以对数组使用$var = []
。 If that syntax throws errors keep using $var = array()
如果该语法引发错误,请继续使用$var = array()
lender_cat
for category_name
, since that's the way to use the WP_Query
我将lender_cat
更改为category_name
,因为这是使用WP_Query
var_dump
or print_r
your results to check if you're getting them 有很多种方法可以调试代码,但是一个简单的检查就是对结果进行var_dump
或print_r
检查,看看是否得到它们 ie 即
<?php
while($wp_query->have_posts()) : $wp_query->the_post();
print_r($post);
endwhile;
Give it a try and let us know! 试试看,让我们知道!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.