普通网友 2019-08-12 12:27
浏览 71
已采纳

如果帖子数据不符合参数,如何排除某些帖子在wp_query中显示?

I am fairly new to PHP so this might be a simple fix. 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.

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; ;
} ?>
  • 写回答

1条回答 默认 最新

  • dongpo2014 2019-08-12 20:03
    关注

    You can add the $creditscore value to your WP_Query. Check the documentation for Custom Field (post meta) Parameters

    i.e.

    $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.

    1. Depending on the PHP version you're using you can use $var = [] for arrays. If that syntax throws errors keep using $var = array()
    2. I added the comparison with the credit score in the Query, so you don't retrieve results you don't need.
    3. I changed the lender_cat for category_name, since that's the way to use the WP_Query
    4. There are elegant ways to debug your code, but a simple check is to var_dump or print_r your results to check if you're getting them

    i.e.

    <?php
    
    while($wp_query->have_posts()) : $wp_query->the_post();
        print_r($post);
    endwhile;
    

    Give it a try and let us know!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口