top of page

Meta Query and Tax Query With AJAX Filter

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Feb 1, 2021
  • 1 min read


add_action('wp_ajax_get_all_jobs',        'get_all_jobs_callback');
add_action('wp_ajax_nopriv_get_all_jobs', 'get_all_jobs_callback');

function get_all_jobs_callback(){

	/*echo "<pre>";
	print_r($_POST);*/

	$paged        = sanitize_text_field($_POST['pageno']);
	$job          = sanitize_text_field($_POST['job']);
    $department   = sanitize_text_field($_POST['department']);
    $location     = sanitize_text_field($_POST['location']);


	$tax_query = array();

	if($department){
		array_push($tax_query, 
				array(
                'taxonomy'  => 'by-department',
                'terms'     =>  $department,
                'field'     => 'slug',
		    	)
		    );
		
	} 	


	if($location){
		array_push($tax_query, 
				array(
                'taxonomy'  => 'by-location',
                'terms'     =>  $location,
                'field'     => 'slug',
		    	)
		    );
		
	} 	


	if($tax_query){

		$args = array(
			'post_type'      	=> 'jobs',
			'posts_per_page' 	=> -1,
			'post_status'    	=> 'publish',
			'paged'				=> $paged,
			'tax_query'         => $tax_query,
			'post__in'          => array($job)				
		);	

		if($job == ""){
			unset($args['post__in']);
		}

	}
	else{
		$args = array(
			'post_type'      	=> 'jobs',
			'posts_per_page' 	=> -1,
			'post_status'    	=> 'publish',
			'paged'				=> $paged,
			'post__in'          => array($job)				
		);	

		if($job == ""){
			unset($args['post__in']);
		}
	}



	$loop = new WP_Query( array_filter($args) );

	if($loop->have_posts()){
		
		while ($loop->have_posts()) {

			$loop->the_post();

	?>

				<div class="list-item-row three-col" data-aos="fade-up">
				    <div class="text-box">
				        <div class="col">
				            <h6><?php echo get_field('job_name_label','option'); ?></h6>
				            <h4><a href="<?php echo get_the_permalink(); ?>"><?php  echo get_the_title(); ?></a></h4>
				        </div>
				        <div class="col">
				            <h6><?php echo get_field('job_department_label','option'); ?></h6>
				            <h4><a href="<?php echo get_the_permalink(); ?>"><?php echo get_department_by_job(get_the_ID()); ?></a></h4>
				        </div>
				        <div class="col">
				            <h6><?php echo get_field('job_location_label','option'); ?></h6>
				            <h4><a href="<?php echo get_the_permalink(); ?>"><?php echo get_location_by_job(get_the_ID()); ?></a></h4>
				        </div>
				    </div>
				    <div class="btn-box">
				        <a href="mailto:<?php echo get_field('email_address_for_job_application',get_the_ID() ); ?>&subject=<?php  echo get_the_title(); ?>,<?php echo get_department_by_job(get_the_ID()); ?>" class="btn">
				            <span class="text"><?php echo get_field('apply_job_label','option'); ?></span>
				        </a>
				    </div>
				</div>

	<?php

		}
	}

	wp_die(); 



}



Another Example




	<?php
				$careerqry    = array(
                    'post_type'      => 'post',
                    'posts_per_page' => -1,
                    'post_status'    => 'publish',
                    'meta_query'     => array(
                    		array(

                    			'key'     => 'is_featured',
                    			'value'   => 1,
                    			'compare' => '='
                    		)
                    )
                );

                $careers  = new WP_Query($careerqry);

                if($careers->have_posts()){
                    while ($careers->have_posts()) {
                        $careers->the_post();
                        $featured_img_url = get_the_post_thumbnail_url();
                ?>





                <?php
                     }
                     wp_reset_query();
                  }
                ?>

 
 
 

Recent Posts

See All
Duplicate Category in Wordpress

The following code helps to duplicate the default wordpress category by help of code. // Add Duplicate option for categories in WordPress...

 
 
 
C#.NET and Its Associated Codes

1- Creating an Desktop Application in C#.NET C# ADO.NET & MySQL MAIN C# and MySQL ADO.NET with Crystals Reports Ready Code 2- Handling...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page