简体   繁体   中英

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. 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; ;
} ?>

You can add the $creditscore value to your 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.

  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

ie

<?php

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

Give it a try and let us know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM