0

add_shortcode( 'portfolio_shortcode', 'portfolio_shortcode' );
function portfolio_shortcode( $atts ) {
 $atts = shortcode_atts(
   array(
    'post_type' => 'project',
    'tax_name' => 'project_category',
    'posts_per_page' => '-1',
    'filter' =>'default',
   ),
   $atts,
   'show_post'
  );
    ob_start();?>
    <section id="portfolio" class="clearfix cp_<?php echo $atts['post_type'];?>">
      <div class="container_port">
        <!-- <header class="section-header">
          <h3 class="section-title">Our Portfolio</h3>
        </header> -->
        <?php
        $args = array('taxonomy' => $atts['tax_name'], 'post_type' => $atts['post_type'],);
        $cats = get_categories($args);
        $def_args = array('post_type' => $atts['post_type'],'posts_per_page' => -1,);
        ?>
        <div class="et_pb_row">
          <div class="et_pb_column_4_4 center_align">
           <?php if($atts['filter']=='custom'){
            $args_day = array('taxonomy' => 'weekday', 'post_type' => $atts['post_type'],'hide_empty'=> false,);
          $cats_days = get_categories($args_day);
           $args_location = array('taxonomy' => 'activity-location', 'post_type' => $atts['post_type'],'hide_empty'=> false,);
          $cats_locations = get_categories($args_day);
           $args_age = array('taxonomy' => 'age', 'post_type' => $atts['post_type'],'hide_empty'=> false,);
          $args_ages = get_categories($args_age);
           $args_place = array('taxonomy' => 'activity-place', 'post_type' => $atts['post_type'],'hide_empty'=> false,);
          $args_places = get_categories($args_place);
           $args_price = array('taxonomy' => 'activity-price', 'post_type' => $atts['post_type'],'hide_empty'=> false,);
          $args_prices = get_categories($args_price);
        ?>
            <form action="" method="get" class="activity_filter">
             <ul>
              <li><select name="qproject_category">
               <option value="All">Type</option>
               <?PHP foreach($cats as $cat) {?>
                <option value="<?php echo $cat->slug; ?>" <?php if($_GET['qproject_category']==$cat->slug){echo 'selected';}?>><?php echo $cat->name; ?></option>
               <?php }?>
              </select></li>
              <li><select name="qweekday">
               <option value="All">Day of the week</option>
               <?PHP foreach($cats_days as $cats_day) {?>
                <option value="<?php echo $cats_day->slug; ?>" <?php if($_GET['qweekday']==$cats_day->slug){echo 'selected';}?>><?php echo $cats_day->name; ?></option>
               <?php }?>
              </select></li>
              <li><select name="qage">
               <option value="All">Age</option>
               <?PHP foreach($args_ages as $args_age) {?>
                <option value="<?php echo $args_age->slug; ?>" <?php if($_GET['qage']==$args_age->slug){echo 'selected';}?>><?php echo $args_age->name; ?></option>
               <?php }?>
              </select></li>
              <li><select name="qactivity_place">
               <?PHP foreach($args_places as $args_plac) {?>
                <option value="<?php echo $args_plac->slug; ?>" <?php if($_GET['qactivity_place']==$args_plac->slug){echo 'selected';}?>><?php echo $args_plac->name; ?></option>
               <?php }?>
              </select></li>
              <li><select name="qactivity-location">
               <option value="All">Location</option>
               <?PHP foreach($cats_locations as $cats_location) {?>
                <option value="<?php echo $cats_location->slug; ?>" <?php if($_GET['qactivity-location']==$cats_location->slug){echo 'selected';}?>><?php echo $cats_location->name; ?></option>
               <?php }?>
              </select></li>
              <li><select name="qactivity_price">
               <?PHP foreach($args_prices as $args_pric) {?>
                <option value="<?php echo $args_pric->slug; ?>" <?php if($_GET['qactivity_price']==$args_pric->slug){echo 'selected';}?>><?php echo $args_pric->name; ?></option>
               <?php }?>
              </select></li>
             </ul>
             <input class="btn form-submit" type="submit" id="submit-activities" value="Filter">
             <a href="<?php echo get_post_type_archive_link( $atts['post_type'] ); ?>" class="btn form-reset">Show all</a>
            </form>
           <?php }else{?>
            <ul id="portfolio-flters">
              <li data-filter="*" class="filter-active">All</li>
              <?PHP foreach($cats as $cat) {
                //var_dump($cat); ?>
              <li data-filter=".<?php echo $cat->slug; ?>"><?php echo $cat->name; ?></li>
              <?PHP }?>
            </ul>
         <?php }?>
          </div>
        </div>
        <div class="et_pb_row portfolio-container et_pb_gutters2">
           <?php 
            if((isset($_GET['qproject_category']) && $_GET['qproject_category']!='All') || (isset($_GET['qweekday']) && $_GET['qweekday']!='All') || (isset($_GET['qage']) && $_GET['qage']!='All') || (isset($_GET['qactivity-location']) && $_GET['qactivity-location']!='All') || isset($_GET['qactivity_price'])|| isset($_GET['qactivity_place'])){
             $def_args['tax_query']= array('relation' => 'OR',);
             if($_GET['qproject_category']!='All'){
              $def_args['tax_query'][] = array(
               array(
             'taxonomy' => 'project_category',
             'field'    => 'slug',
             'terms'    => $_GET['qproject_category'],
         ),
              );
             }
             if($_GET['qweekday']!='All'){
              $def_args['tax_query'][] = array(
               array(
             'taxonomy' => 'weekday',
             'field'    => 'slug',
             'terms'    => $_GET['qweekday'],
         ),
              );
             }
             if($_GET['qage']!='All'){
              $def_args['tax_query'][] = array(
               array(
             'taxonomy' => 'age',
             'field'    => 'slug',
             'terms'    => $_GET['qage'],
         ),
              );
             }
             if($_GET['qactivity-location']!='All'){
              $def_args['tax_query'][] = array(
               array(
             'taxonomy' => 'activity-location',
             'field'    => 'slug',
             'terms'    => $_GET['qactivity-location'],
         ),
              );
             }
             if($_GET['qactivity_place']){
              $def_args['tax_query'][] = array(
               array(
             'taxonomy' => 'activity-place',
             'field'    => 'slug',
             'terms'    => $_GET['qactivity_place'],
         ),
              );
             }
             if($_GET['qactivity_price']){
              $def_args['tax_query'][] = array(
               array(
             'taxonomy' => 'activity-price',
             'field'    => 'slug',
             'terms'    => $_GET['qactivity_price'],
         ),
              );
             }
            }
                         $query = new WP_Query($def_args);
            // $query = new WP_Query( array(
            // 'post_type' => $atts['post_type'],
            // 'posts_per_page' => -1,
           //) );
          if ( $query->have_posts() ) {
          $count=0;
          while ( $query->have_posts() ) : $query->the_post();
          @$featured_img_url = get_the_post_thumbnail_url($post->ID,'full');
    @$featured_mimg_url = get_the_post_thumbnail_url($post->ID,'medium_large');
          @$categories = get_the_terms( $post->ID, $atts['tax_name']);
          $terms_string = join(' / ', wp_list_pluck($categories, 'name')); ?>
            <div class="et_pb_column et_pb_column_1_3 portfolio-item <?php if (is_array($categories) || is_object($categories)){ foreach( $categories as $category ){echo $category->slug.' ' ;}}?>">
             <a href="<?php echo the_permalink();?>" class="link-text">
               <div class="portfolio-wrap" style="background-image: url(<?php echo $featured_mimg_url;?>);">
                 <div class="portfolio-info">
                     <h4><?php the_title();?></h4>
                     <span class="project_cat"><?php echo $terms_string;?></span>
                     <a href="<?php echo the_permalink();?>">
                      <div class="port_link"><span class='et-pb-icon'></span></div>
                  </a>
                 </div>
               </div>
           </a>
            </div>
          <?php $count++;  endwhile;
            wp_reset_postdata(); 
        }
          ?>
        </div>
      </div>
    </section>
         <?php wp_reset_query();
  $myvariable = ob_get_clean();
    return $myvariable;
}

Jagdish Sarma Asked question October 29, 2021
Add a Comment