admin管理员组

文章数量:1335363

I need help with adding a second category filter to a site. I have the first one working but not sure how to add a second one. The first one will choose the type of podcasts and the second needs to choose by a name. I already have the post types and categories setup in the functions.php file for the podcasts categories so would I need to add anything additional there since it will be using the same categories just filtering them in a different way?

And would I need to set them up as a parent category with child categories for each filter? For example one parent name with the Type of Podcasts to filter and one with the Names to filter?

Also how do I specify each filter to show just one with types in the dropdown and one with just names in the second dropdown?

    <?php
$filter = get_query_var( 'podcast_category_filter' );

$args = array(
    'taxonomy'          => 'podcast_category',
    'selected'          => sanitize_text_field( $filter ),
    'name'              => 'podcast_category_filter',
    'hide_empty'        => false,  
    'orderby'           => 'name', 
    'value_field'       => 'slug',
    'echo'              => false,  
    'show_option_all'   => 'All podcasts',
    'option_none_value' => 0       
);
$select_category_html = wp_dropdown_categories( $args );
?>

<form method='GET'>
    <?php echo $select_category_html ?>
    <button type='submit'>Filter</button>
</form>

<div class="podcasts">

<ul>
    <?php $tax_query = array();

    
    if ( ( 0 != $filter ) && is_string( $filter ) && ( '' != $filter ) ) {
        $tax_query = array(
            array(
                'taxonomy' => 'podcast_category',
                'field'    => 'slug',
                'terms'    => sanitize_text_field( $filter ),
            )
        );
    }

    // Create a secondary query to avoid using the main query.
    $args = array_merge(
        array( 'post_type' => 'podcast' ),
        array( 'tax_query' => $tax_query )
    );
    $podcast_query = new WP_Query( $args );

    if ( $podcast_query->have_posts() ) :
        while ( $podcast_query->have_posts() ) : $podcast_query->the_post(); ?>

<li>
</li>
</ul>
</div>

I appreciate any help! Thank you!

I need help with adding a second category filter to a site. I have the first one working but not sure how to add a second one. The first one will choose the type of podcasts and the second needs to choose by a name. I already have the post types and categories setup in the functions.php file for the podcasts categories so would I need to add anything additional there since it will be using the same categories just filtering them in a different way?

And would I need to set them up as a parent category with child categories for each filter? For example one parent name with the Type of Podcasts to filter and one with the Names to filter?

Also how do I specify each filter to show just one with types in the dropdown and one with just names in the second dropdown?

    <?php
$filter = get_query_var( 'podcast_category_filter' );

$args = array(
    'taxonomy'          => 'podcast_category',
    'selected'          => sanitize_text_field( $filter ),
    'name'              => 'podcast_category_filter',
    'hide_empty'        => false,  
    'orderby'           => 'name', 
    'value_field'       => 'slug',
    'echo'              => false,  
    'show_option_all'   => 'All podcasts',
    'option_none_value' => 0       
);
$select_category_html = wp_dropdown_categories( $args );
?>

<form method='GET'>
    <?php echo $select_category_html ?>
    <button type='submit'>Filter</button>
</form>

<div class="podcasts">

<ul>
    <?php $tax_query = array();

    
    if ( ( 0 != $filter ) && is_string( $filter ) && ( '' != $filter ) ) {
        $tax_query = array(
            array(
                'taxonomy' => 'podcast_category',
                'field'    => 'slug',
                'terms'    => sanitize_text_field( $filter ),
            )
        );
    }

    // Create a secondary query to avoid using the main query.
    $args = array_merge(
        array( 'post_type' => 'podcast' ),
        array( 'tax_query' => $tax_query )
    );
    $podcast_query = new WP_Query( $args );

    if ( $podcast_query->have_posts() ) :
        while ( $podcast_query->have_posts() ) : $podcast_query->the_post(); ?>

<li>
</li>
</ul>
</div>

I appreciate any help! Thank you!

Share Improve this question asked Nov 20, 2024 at 18:00 PeggyPeggy 671 silver badge13 bronze badges 4
  • So both the podcast types and the podcast names, are podcast_category items? Then you will have to work with parent and child categories, yes, because otherwise how would you differentiate between them in the first place? – C3roe Commented Nov 21, 2024 at 8:25
  • Thank you! So how would I adjust the above code to include another dropdown of categories and set the two apart with the parent categories? – Peggy Commented Nov 21, 2024 at 12:47
  • I still can't tell what your actual scenario here is, so please give a proper explanation. Did you stick type and name into one single category? If so, a)_why_?, and b) what does that actually look like? Is the selection of a specific type then somehow supposed to only show names that belong to that type, or still all names? – C3roe Commented Nov 21, 2024 at 13:35
  • Thank you for your help, I ended up figuring it out. – Peggy Commented Nov 21, 2024 at 15:49
Add a comment  | 

1 Answer 1

Reset to default 0

I figured it out. I added parent and child categories and then added another Query but included parent => cat_ID to each array and added the parent ID's and it worked.

本文标签: phpI need to add a second category filterStack Overflow