0

enter image description here

Look to this images For example chose "Ajna" then select "Residential and Hotel Apartment" after click Search Properties => I need to show items that have these categories

<ul id="actionslist" class="dropdown-menu filter_menu" role="menu" aria-labelledby="adv_actions">
    <li role="presentation" data-value="all">Project Types</li>
    <?php 
    // Get Projects
    $tab_terms_types      =   array();
    $terms_types          =   get_terms( 'property_action_category', array(
                        'hide_empty' => false,
                    ) );
    
    foreach( $terms_types as $single_term_type ){
        $args_types = array(
            'post_type'         =>  'estate_property',
            'author'        =>   -1,    
            'posts_per_page'    =>  -1,
            'post_status'       => 'publish',
            'meta_key'          => 'prop_featured',
            'orderby'           => 'meta_value',
            'order'             => 'DESC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'property_action_category',
                    'field'    => 'term_id',
                    'terms'    => $single_term_type->term_id,
                ),
            ),
            'fields' => 'ids'
        );
        $all_posts_types = get_posts( $args_types );
        if( count( $all_posts_types ) > 0 )
    $tab_terms_types[ $single_term_type->term_id ] = array( 'name' => $single_term_type->name, 'slug' => $single_term_type->slug, 'count' => count( $all_posts ) );

    }
        
    foreach($tab_terms_types as $term){
            ?>
    <div>
        <input type="checkbox" id="<?php echo $term['slug']; ?>" class="types-input"
            name="filter_search_action[]" value="<?php echo $term['slug']; ?>"
            data-value="<?php echo $term['slug']; ?>">
        <label for="<?php echo $term['slug']; ?>"><?php echo $term['name']; ?></label>
    </div>

    <? } ?>
 </ul>

jQuery Ajax

const typesArray = [];
$(".types-input").click(function(event){
    if($(this).prop("checked") == true){

        typesArray.push(jQuery(event.target).attr('data-value'));
        console.log(typesArray);
    }
})
$(".types-input").change(function(){
    const ajaxurl = ajaxcalls_vars.admin_url + 'admin-ajax.php';
    const nonce = jQuery('#wpestate_tab_stats').val();
    
    $.ajax({
        method: "GET",
        url: ajaxurl,
        data: {
            'action': 'wpestate_load_types',
            'type_slug': typesArray,
            'security': nonce
        },
        success: function(data){
            $(".res-type").html(data);
            console.log(data);
            console.log(typesArray);
        }
    })
})

functions.php

add_action( 'wp_ajax_wpestate_load_types', 'wpestate_load_types' );
add_action( 'wp_ajax_nopriv_wpestate_load_types', 'wpestate_load_types' );

function wpestate_load_types(){
    global $wpdb;
    $typesVal = $_GET["type_slug"];

    foreach($typesVal as $k => $v){
        add_query_arg( $k, $v);
    }

    wp_die();
}

1 Answer 1

1

This really needs more context in what apple and banana are referring to and what you're trying to accomplish and how you've already tried to accomplish it...

You could do a custom query and set the parameters in terms based on whatever was selected in the multiple choice. This is assuming that A. fruit is a CPT and that B. "apple" and "banana" are custom taxonomies of "type" associated with the fruit CPT.

$args = array(
    'post_type' => 'fruit',
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field'    => 'slug',
            'terms'    => array( 'apple', 'banana' )
        )
    )
);
$query = new WP_Query( $args );

New queries can cause issues with pagination if that's a concern so use with caution.

You may also want to consider checking out the pre_get_posts filter, here is an article/question-answer thread that helped me get a form with custom options that queries a CPT and it's taxonomy set up: Wordpress custom search form with pre_get_posts not work

(just noticed the ajax part)

You can also make requests through the wp api and get back multiple categories from a post type like so:

/wp-json/wp/v2/posttype?category=3,4

Replace posttype with your post type name and category with the taxonomy name you are trying to access, finally replace 3,4 with whatever the ID's of the category are. So your query might look something like below, assuming fruit is the name of the post type and 5 is the ID of apple and 9 is the ID of banana ( for example )

/wp-json/wp/v2/fruit?category=5,9
7
  • apple and banan are in the same category for example I have in category => apple, banan and orange I need when client select apple and banana cateory, it shows details of them Apr 6, 2022 at 20:52
  • @MarcoTalaat see the part I included about making requests to the wp api, this may be what you are looking for, you'll need to figure out the category ID of apple, banana or whatever and include those, you'll also need to figure a way to create the request based on what the user has selected which shouldn't be too difficult. Apr 6, 2022 at 21:01
  • Check my update question please Apr 6, 2022 at 21:10
  • @MarcoTalaat you'll need to update your question with the code that you're using or that is not working as you expect. The screenshot does not help. Apr 6, 2022 at 21:17
  • I haven't code because I asked if the url parameter can accept to values or not Apr 6, 2022 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.