[英]How to exclude certain posts from showing up in wp_query if the post data does not fit parameters?
我對PHP相當陌生,因此這可能是一個簡單的修復。 我正在編輯一個插件,並嘗試將其修改為基於一組輸入的位置,我只想輸出其元數據適合輸入參數的某些帖子,而不顯示其數據不適合的帖子。 如何排除數據不適合輸出的帖子?
到目前為止,我已經做完了所有事情,但是被困在哪里,如果輸入的數據不適合后元數據,它將不會顯示。 我猜想我將需要使用$_post == null
函數,並嘗試了許多變體,但是沒有任何效果。
這是我堅持的代碼行:
<?php if ( $creditscore >= esc_attr($min_credit_score) ) { echo "good";} else { $_Post == null; ;
} ?>
這是完整的代碼:
<?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; ;
} ?>
您可以將$creditscore
值添加到WP_Query
。 檢查文檔中的“ 自定義字段”(后元)參數
即
$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);
注釋 :既然您提到您是PHP的新手,我將添加一些注釋。
$var = []
。 如果該語法引發錯誤,請繼續使用$var = array()
lender_cat
更改為category_name
,因為這是使用WP_Query
var_dump
或print_r
檢查,看看是否得到它們 即
<?php
while($wp_query->have_posts()) : $wp_query->the_post();
print_r($post);
endwhile;
試試看,讓我們知道!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.