0

I have Three Fields in the form, first is Default WordPress Category and two custom taxonomies. When I try to Search a Single field "Selected Attribute" Works Correct but on category, selection taxonomy selected value unselect and do not show selected attribute. Below is the Code:

    <form method="get" action="<?php echo get_site_url(); ?>" class="">
    
    
    <!--categories-->   
    <!--default categories dropdown-->
    <?php
    $categoryselected = get_queried_object();
    $categories = wp_dropdown_categories(array(
        'taxonomy' => 'category',
        'post_type' => 'post',
        'show_option_all' => 'All Categories',
        'orderby' => 'name',
        'echo' => 1,
        'select' => $cat,
        'name' => 'cat',
        'hierarchical' => true,
        'class' => 'cat-dropdown form-control',
        'id' => 'custom-cat-drop',
        'value_field' => 'term_id',
         'post_status' => 'publish',
       'no_found_rows' => true,
       
       /*when I selected this category other fields selected attributes not working*/
       'selected' => $categoryselected->term_id
    ));
    ?>
    
    <!--location-->
    <?php
    $current_category = get_queried_object();
    wp_dropdown_categories(array(
        'taxonomy' => 'location',
        'post_type' => 'post',
        'show_option_all' => 'Select Location',
        'orderby' => 'name',
        'echo' => 1,
        'name' => 'location',
        'hierarchical' => true,
        'class' => 'cat-dropdown form-control',
        'id' => 'custom-cat-dr
        'value_field' => 'name',
         'post_status' => 'publish',
       'no_found_rows' => true,
       

/*in Single Field Search it work correctly but on above category selection attribute not worked*/
       'selected' => $current_category->name
    ));
    ?>
    
    <!--job_type-->
    <?php
    $current_category = get_queried_object();
        wp_dropdown_categories( array(
                        'taxonomy'          => 'job_type',
                        'post_type' => 'post',
                        'show_option_all'  => 'Job Type',
                        'option_none_value' => '',
                        'echo' => 1,
                        'name'              => 'job_type',
                        'id'                => 'custom-cat-drop',
                        'select'          => $cat,
                        'value_field' => 'name',
                        'class' => 'cat-dropdown form-control',
                         'post_status' => 'publish',
                         'hierarchical' => true,
                         'no_found_rows' => true,

/*in Single Field Search it work correctly but on above category selection attribute not worked*/
                          'selected' => $current_category->name
                    ) );
                ?>
    
    <input type="hidden" value="post" name="post_type" />
    
    <button type="submit" class="btn-search">Find Job <i class="fa fa-search"></i></button>
    
    </form>
2
  • The selected attribute is the same for each of three queries. And it's impossible to say what is its value because it is unknown in which template file the code resides.
    – Max Yudin
    Jan 23, 2022 at 19:31
  • Please Check the Template placements.iadm.edu.pk and when we search we land on a search template where the selected attribute has not to work. The above code is created in the template part and applied on Home - Search and other required templates.
    – stepiadm
    Jan 24, 2022 at 11:02

1 Answer 1

0

Firstly, you have the same 'selected' parameter for all dropdowns. Secondly, get_queried_object() returns different objects in different template files. See User Contributions under get_queried_object() description in the Code Reference.

I've followed the link you've provided, so I suggest to use get_query_var(). It's not enough information in the question to suggest something else.

<?php

/* Category dropdown */
$category_args = array(
    'selected' => get_query_var('cat'),
    /* Remaining parameters here */
)
wp_dropdown_categories( $category_args );


/* Location dropdown */
$location_args = array(
    'selected' => get_query_var('location'),
    /* Remaining parameters here */
)
wp_dropdown_categories( $location_args );


/* Job Type dropdown */
$job_type_args = array(
    'selected' => get_query_var('job_type'),
    /* Remaining parameters here */
)
wp_dropdown_categories( $job_type_args );

Also:

  • Check all wp_dropdown_categories() arguments as you have a mess there.
  • I've added the search values directly to URL and I see wrong search results as the taxonomies are nested in a weird way.
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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