Filtering by Custom Taxonomy and Meta Fields in WordPress Produces Inconsistent Results

[ad_1]

I'm building a custom WordPress theme for a job listing site, and I'm encountering an issue with filtering job listings. My site has custom post types and taxonomies for job categories and job types.

I can successfully filter by job categories using a query like:

http://example.com/?category=tech&salary_min=0&salary_max=50000

However, when I try to add a job type filter, the URL structure changes to:

http://example.com/job-type/freelance/?category=tech&salary_min=0&salary_max=50000

This causes the query to only filter by job type, ignoring other filters like job category and salary range.

Here’s a simplified version of the relevant code I'm using:

“`php

 <section class="job-listings">

            <?php
            $keywords = isset($_GET['keywords']) ? sanitize_text_field($_GET['keywords']) : '';
            $category = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
            $job_types = isset($_GET['job_type']) ? (array) $_GET['job_type'] : [];
            $date_posted = isset($_GET['date-posted']) ? sanitize_text_field($_GET['date-posted']) : '';
            $salary_min = isset($_GET['salary_min']) ? intval($_GET['salary_min']) : 0;
            $salary_max = isset($_GET['salary_max']) ? intval($_GET['salary_max']) : 20000;

            $args = array(
                'post_type' => 'job',
                'posts_per_page' => 10,
            );

            if ($keywords) 
                $args['s'] = $keywords;
            

            $tax_query = array('relation' => 'AND');

            if ($category) 
                $tax_query[] = array(
                    'taxonomy' => 'job_category',
                    'field'    => 'slug',
                    'terms'    => $category,
                );
            

            if ($job_types) 
                $tax_query[] = array(
                    'taxonomy' => 'job_type',
                    'field'    => 'slug',
                    'terms'    => $job_types,
                );
            

            if (!empty($tax_query)) 
                $args['tax_query'] = $tax_query;
            

            if ($date_posted) 
                switch ($date_posted) 
                    case 'last-hour':
                        $args['date_query'] = array(
                            array(
                                'after' => '1 hour ago',
                            ),
                        );
                        break;
                    case 'last-24-hours':
                        $args['date_query'] = array(
                            array(
                                'after' => '24 hours ago',
                            ),
                        );
                        break;
                    case 'last-7-days':
                        $args['date_query'] = array(
                            array(
                                'after' => '7 days ago',
                            ),
                        );
                        break;
                    case 'last-14-days':
                        $args['date_query'] = array(
                            array(
                                'after' => '14 days ago',
                            ),
                        );
                        break;
                    case 'last-30-days':
                        $args['date_query'] = array(
                            array(
                                'after' => '30 days ago',
                            ),
                        );
                        break;
                
            

            $meta_query = array();

            if ($salary_min || $salary_max) 
                $meta_query[] = array(
                    'key'     => '_job_salary',
                    'value'   => array($salary_min, $salary_max),
                    'type'    => 'NUMERIC',
                    'compare' => 'BETWEEN',
                );
            

            if (!empty($meta_query)) 
                $args['meta_query'] = $meta_query;
            

            $job_query = new WP_Query($args);

            if ($job_query->have_posts()) :
                while ($job_query->have_posts()) : $job_query->the_post();
                    $job_types = get_the_terms(get_the_ID(), 'job_type');
                    $job_category = get_the_terms(get_the_ID(), 'job_category');
                    $job_category_names = $job_category ? join(', ', wp_list_pluck($job_category, 'name')) : '';
                    $job_type_names = $job_types ? join(', ', wp_list_pluck($job_types, 'name')) : '';
                    $salary = get_post_meta(get_the_ID(), '_job_salary', true);

                    $expiration_date = get_post_meta(get_the_ID(), '_job_expiration_date', true);
                    $is_available = strtotime($expiration_date) >= current_time('timestamp');
                    $salary_float = floatval(preg_replace('/[^\d.]/', '', $salary));

                    ?>


                    <div class="job-card">
                    <div class="job-card-title">
                        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <span class="availablity <?php echo $is_available ? 'available' : 'not-available'; ?>">
                            <i class="fas fa-calendar-alt"></i> <?php echo $is_available ? 'Available' : 'Not Available'; ?>
                        </span>
                    </div>

                        <div class="job-card-meta">
                            <p class="job-category"><i class="fi fi-rr-briefcase"></i><?php echo esc_html($job_category_names); ?></p>
                            <span><i class="fas fa-clock"></i> <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></span>
                            <p class="job-salary"><i class="fa fa-money"></i><?php echo esc_html($salary); ?></p>
                        </div>
                    </div>

                <?php endwhile;
            else :
                echo '<p>No jobs found</p>';
            endif;

            wp_reset_postdata();
            ?>
        </section>```

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer