Query WordPress custom post by multiple conditions

Can anybody help me query custom post in WordPress?
I want to query multiple conditions:

if(get_post_meta(get_the_ID(), 'nt_status', true) == "for-sale" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "85" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "4847.40" && has_term( 'mieszkanie', 'propertytype' ) && has_term( 'poznan', 'location' )) :?>
        return true
   <?php endif; ?>

   <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "for-sale" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "85" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "4126.68" && has_term( 'mieszkanie', 'propertytype' ) && has_term( 'powiat-poznanski', 'location' )) :?>
        return true
   <?php endif; ?>

    <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "for-sale" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "110" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "4847.40" && has_term( 'dom', 'propertytype' ) && has_term( 'poznan', 'location' )) :?>
        return true
   <?php endif; ?>

    <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "for-sale" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "110" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "4126.68" && has_term( 'mieszkanie', 'propertytype' ) && has_term( 'powiat-poznanski', 'location' )) :?>
        return true
   <?php endif; ?>

   <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "pierwotny" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "85" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "5924,60" && has_term( 'mieszkanie', 'propertytype' ) && has_term( 'poznan', 'location' )) :?>
        return true
   <?php endif; ?>

   <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "pierwotny" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "85" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "5043,72" && has_term( 'mieszkanie', 'propertytype' ) && has_term( 'powiat-poznanski', 'location' )) :?>
        return true
   <?php endif; ?>

    <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "pierwotny" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "110" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "5924,60" && has_term( 'dom', 'propertytype' ) && has_term( 'poznan', 'location' )) :?>
        return true
   <?php endif; ?>

    <?php if(get_post_meta(get_the_ID(), 'nt_status', true) == "pierwotny" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział" && get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "110" && get_post_meta(get_the_ID(), 'nt_listprice', true)/get_post_meta(get_the_ID(), 'nt_plot_size', true) <= "5043,72" && has_term( 'dom', 'propertytype' ) && has_term( 'powiat-poznanski', 'location' )) :?>
        return true
   <?php endif; ?>

I have template and know that it can be done by $args['meta_query'][] = array( with AND & OR relative) but maybe someone have simpler solution?

Related posts

1 comment

  1. Is there any option to use this conditions to store to property meta information? Something like: if condition true -> property meta = 1

    Something like this:

    if(get_post_meta(get_the_ID(), 'nt_status', true) == "for-sale" && get_post_meta($post->ID, "nt_wlasnosc", true)!= "udział") :?>
    get_post_meta(get_the_ID(), 'nt_mdm', true) = 1
    <?php endif; ?>
    

    I don’t know how can I store this info to query?

    get_post_meta(get_the_ID(), ‘nt_mdm’, true) = 1 <- this doesn’t work

    And then simple query:

    $args['meta_query'][] = array(
        'key' => 'nt_mdm',
        'value' => 1,
        'compare' => 'IN'
    );
    

Comments are closed.