0

//Shortcode Created
add_shortcode( 'post_view', 'dtwd_special_collection_view' );
function dtwd_special_collection_view( $atts ) {
    $atts = shortcode_atts(
       array(
        'post_type' => 'post',
        'posts_per_page' => '-1',
        'post_status' => 'publish',
         'pid' => '', 
         'bid' => '', 
        'order'                => 'DESC'
       ),
       $atts,
       'show_post'
      );
    ob_start();
    $squery = array(
        'post_type'      => $atts['post_type'],
        'posts_per_page' => -1,
    );
    if($atts['bid']!=''){
        $squery['meta_query'] = array(
            array(
                'key'     => 'builder_name', // ACF field name for the relationship
                'value'   => $atts['bid'],
                'compare' => '=',
            ),
        );
    }
    if($atts['pid']!=''){
        $squery['p'] = $atts['pid'];
    }
    //var_dump($squery);
    $query = new WP_Query( $squery );
    if ( $query->have_posts() ) { ?>
        <div class="post-collection old <?php echo $atts['pid'];?>">
        <?php while ( $query->have_posts() ) : $query->the_post(); 
        @$featured_mimg_url = get_field('collection_photo', get_the_ID());?>
               <div class="dyn-filter"></div>
<?php $collection = ob_get_clean();
    return $collection;
    }
}
//Added Js in Footer
add_action( 'wp_footer', 'ajax_fetch_posts_coll' );
function ajax_fetch_posts_coll() {
?>
<style>.dyn-filter{display: none;}</style>
<script>
jQuery(document).on('click', '.prcing-toggle, .builder-names label.b-names', function(e) {
 setTimeout(function(){ 
     get_price = jQuery('.prcing-toggle').find('.active').text().trim();
     //console.log(get_price);
     get_type = jQuery('.builders-box input[type="checkbox"]:checked').serialize();
     if(get_type === 'all=All' && get_price === 'Low to High'){
      console.log(get_type+' - '+get_price);
      jQuery('.post-collection.old').show();
      jQuery('.dyn-filter').hide();
      return;
     }else{
      jQuery.ajax({
          url: '<?php echo admin_url('admin-ajax.php'); ?>',
          type: 'post',
          data: { 
           action: 'data_fetch_coll', 
           get_price_sort: get_price,
           get_type_sort : get_type,
          },
          success: function(data) {
              console.log(data);
              jQuery('.post-collection.old').hide();
              jQuery('.dyn-filter').html(data).show();
          },
          error: function() {
              console.log("Error");
       }
      });
  }
 }, 1000);
});
</script>
<?php
}
//Ajax Action
add_action('wp_ajax_data_fetch_coll' , 'data_fetch_coll');
add_action('wp_ajax_nopriv_data_fetch_coll','data_fetch_coll');
function data_fetch_coll(){
 $get_price_sort = $_POST["get_price_sort"];
 $get_type_sort = $_POST["get_type_sort"];
 if($get_price_sort=='Low to High'){
  $order = 'ASC';
  $mkey = 'price_ranges_for_sort';
 }else{
  $order = 'DESC';
  $mkey = 'high_price_range_for_sort';
 }
 //echo $get_price_sort.' -Response- '.$get_type_sort.' '.$order;
 parse_str($get_type_sort, $get_type_sortData);
 $bid = array();
 $all_res = 0;
 foreach ($get_type_sortData as $key => $value) {
  $builder = urldecode($value);
  if($builder != 'All'){
   $bid[] = c2_get_post_id_by_title($builder);
      //echo "Selected builder: $get_id $builder<br>";
  }else{
   $all_res = 1;
  }
 }
 $query = array(
        'post_type'      => 'home-collection',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_key'       => $mkey, 
     'orderby'        => 'meta_value', 
     'order'          => $order, 
    );
    if($get_type_sort!='' && $all_res != 1){
        $query['meta_query'] = array(
            array(
                'key'     => 'builder_name', // ACF field name for the relationship
                'value'   => $bid,
                'compare' => 'IN',
            ),
        );
    }
       $query = new WP_Query( $query );
    if ( $query->have_posts() ) { ?>
        <div class="builder-price-sorter">
            <div class="prcing-toggle">
              <div class="price-tab">Low to High</div>
              <div class="price-tab active">High to Low</div>
            </div>
        </div>
        <div class="builder-names">
        <div class="builder-toggle">Filter By Builder</div>
        <div class="builders-box">
        <label for="all" class="b-names active">
        <input type="checkbox" id="all" name="all" value="All">
        All</label>
        <label for="Builder4" class="b-names">
        <input type="checkbox" id="Builder4" name="Builder4" value="Builder 4">
        Builder 4</label>
        <label for="Builder5" class="b-names">
        <input type="checkbox" id="Builder5" name="Builder5" value="Builder 5">
        Builder 5</label>
        </div>
        </div>
        <div class="post-collection new <?php echo $atts['pid'];?>">
        <?php while ( $query->have_posts() ) : $query->the_post(); 
                 @$featured_mimg_url = get_field('collection_photo', get_the_ID());
        //$collection_name =  get_field('collection_name', get_the_ID());?>
             <div class="collection-divider"></div>
    <?php endwhile;
 }else{
  echo '<h2 style="text-align:center;">No homes found!</h2>';
 }
 die();
}
//Function to get post id by title
function c2_get_post_id_by_title( string $title = '' ): int {
    $posts = get_posts(
        array(
            'post_type'              => 'post',
            'title'                  => $title,
            'numberposts'            => 1,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false,
            'orderby'                => 'post_date ID',
            'order'                  => 'ASC',
            'fields'                 => 'ids'
        )
    );
     return empty( $posts ) ? get_the_ID() : $posts[0];
}

Jagdish Sarma Asked question February 29, 2024
Add a Comment