$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - Display post after choice two taxonomy terms|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - Display post after choice two taxonomy terms

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far