filters - Display post after choice two taxonomy terms

admin2025-06-04  3

I display post by a term filter (first taxonomy) and it works very well.

I add an other list of terms filter (second taxonomy).

I would like to display posts after choice a term in each taxonomy.

Thank's for your help.

/* First taxonomy's template. It works like a charm. */
$query = new \WP_Query(array(
        'post_type' => 'postwork',
        'tax_query' => array(
            array(
                'taxonomy' => 'workfilter',
                'field' => 'term_id',
                'terms' => get_queried_object_id(),
            )
        )
    ));

/* Second taxonomy's template. It doesn't work I don't know how to recover the first term value */
    $query = new \WP_Query(array(
        'post_type' => 'postwork',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'workfilter', /* first taxonomy */
                'field' => 'term_id',
                'terms' => '', /* Want to get the term */
            ),
            array(
                'taxonomy' => 'workfiltercondition', /* second taxonomy */
                'field' => 'term_id',
                'terms' => get_queried_object_id(),
            )
        )
    ));

    /* Loop to display posts */

    if ( $query->have_posts() ): ?>
        <div class="container-fluid">
            <div class="works-list">
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <div class="works-item">
                        <a href="<?php echo get_permalink(); ?>">
                            <img src="<?php  echo get_the_post_thumbnail_url(); ?>" alt="" />
                            <div class="d-flex justify-content-between works-info">
                                <div>
                                    <h2><?php echo get_the_title(); ?></h2>
                                    <p><?php echo the_field('work_place'); ?></p>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
    <?php endif;

    /* jQuery Script to works with Ajax */

    jQuery(function(){
    var mainContent = jQuery('.container-fluid');
    var catLinks = jQuery('ul.categories-filters li a');

    catLinks.on('click', function(e){

        e.preventDefault();
        el = jQuery(this);
        var value = el.attr("href");
        mainContent.animate({opacity:"0.5"});
        mainContent.load(value + " .works-list", function(){
            mainContent.animate({opacity:"1"});
        });
        jQuery( "li" ).removeClass( "current-cat" );
        jQuery(this).closest('li').addClass("current-cat");
    });
});

I display post by a term filter (first taxonomy) and it works very well.

I add an other list of terms filter (second taxonomy).

I would like to display posts after choice a term in each taxonomy.

Thank's for your help.

/* First taxonomy's template. It works like a charm. */
$query = new \WP_Query(array(
        'post_type' => 'postwork',
        'tax_query' => array(
            array(
                'taxonomy' => 'workfilter',
                'field' => 'term_id',
                'terms' => get_queried_object_id(),
            )
        )
    ));

/* Second taxonomy's template. It doesn't work I don't know how to recover the first term value */
    $query = new \WP_Query(array(
        'post_type' => 'postwork',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'workfilter', /* first taxonomy */
                'field' => 'term_id',
                'terms' => '', /* Want to get the term */
            ),
            array(
                'taxonomy' => 'workfiltercondition', /* second taxonomy */
                'field' => 'term_id',
                'terms' => get_queried_object_id(),
            )
        )
    ));

    /* Loop to display posts */

    if ( $query->have_posts() ): ?>
        <div class="container-fluid">
            <div class="works-list">
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <div class="works-item">
                        <a href="<?php echo get_permalink(); ?>">
                            <img src="<?php  echo get_the_post_thumbnail_url(); ?>" alt="" />
                            <div class="d-flex justify-content-between works-info">
                                <div>
                                    <h2><?php echo get_the_title(); ?></h2>
                                    <p><?php echo the_field('work_place'); ?></p>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
    <?php endif;

    /* jQuery Script to works with Ajax */

    jQuery(function(){
    var mainContent = jQuery('.container-fluid');
    var catLinks = jQuery('ul.categories-filters li a');

    catLinks.on('click', function(e){

        e.preventDefault();
        el = jQuery(this);
        var value = el.attr("href");
        mainContent.animate({opacity:"0.5"});
        mainContent.load(value + " .works-list", function(){
            mainContent.animate({opacity:"1"});
        });
        jQuery( "li" ).removeClass( "current-cat" );
        jQuery(this).closest('li').addClass("current-cat");
    });
});
Share Improve this question asked Jan 28, 2019 at 13:39 user160038user160038 1 1
  • Hmm, what should that code do exactly? – Krzysiek Dróżdż Commented Jan 28, 2019 at 14:14
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to use the filter on the archive page then you have to get the second term id by using the global variable or by passing the hard value of the term id. But if you are using the filter on any other page then you can send the term id through post or get method of the form or using ajax.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748967028a315230.html

最新回复(0)