Custom Fields
Re-Envisioned
Support MB Custom Taxonomy Show only posts For CPT that have specific terms from tax_1 AND tax_2

  • Creator
    Topic
  • #31559
    Rebecca Robertson
    Participant

    This seems relatively simple so maybe I'm overthinking it, but here's the basic gist:

    I have a CPT called destination.
    I have a custom taxonomy called "state". The terms are state names.
    I have a custom taxonomy called "destination-category". The terms are various types of destinations (ie: National Parks, State Parks, Camp Grounds, etc.).

    Each destination post is assigned 1 state and 1 destination category.

    So, for instance, I have three destination posts:
    "Saguaro National Park" and assigned to the state "Arizona" and the destination category "National Park"

    "Yosemite National Park" and assigned to the state of "California" and the destination category of "National Parks"

    "Craggy Wash" assigned to the state of "Arizona" and the destination category of "Dispersed Camping"

    As expected, on the "State" taxonomy page for Arizona it shows all of the destination posts assigned the state taxonomy "arizona" and the taxonomy pages for National Parks and Dispersed Camping do the same thing. That's all good.

    What I want to do is have a taxonomy page that shows destination posts National Parks that are in Arizona etc.

    Right now I'm not overly concerned about how the URL turns out, I'll deal with that later, I just want to show all posts that have share the same state taxonomy term and destination category term on one page. Preferably, I would like to do it in a way that doesn't require me to write a separate statement for every single term permutation.

    On the state taxonomy page I'm using this $state = get_queried_object();
    Is it possible to do something similar to get the results I want? Would I need to use Relationships? I have no idea where to begin!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #31560
    Rebecca Robertson
    Participant

    I should have noted a few things, so I'm adding them here.

    On each state taxonomy term page (ie Arizona, California, Nevada), I have a section in the sidebar that lists various statistics about the state. Each statistic looks something like this:

    "DESTINATION CATEGORY": There are "#" "Destination Category Name" in "State"
    The "Destination Category Name" is suppose to be a link to archive page for that particular destination category in that particular state.

    I found a tutorial that almost works for what I need and added this code to the destination category archive page:

    
    <?php
    // get the currently queried taxonomy term, for use later in the template file
    $destination = get_queried_object();
    ?>
    
    <?php 
    //start by fetching the terms for the animal_cat taxonomy
    $states = get_terms( 'state', array(
        'hide_empty' => 0
    ) );
    ?>
    
    <?php
    // now run a query for each State
    foreach ( $states as $state ) {
    
    // Define the query
    $args = array(
    'post_type' => 'destination',
    'destination-category' => $destination->slug,
    'state' => $state->slug
    );
    
    $query = new WP_Query( $args );
                
    // output the term name in a heading tag                
    echo'<h2>' . $state->name . ' ' . $destination->name . '</h2>'; 
                    
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php get_template_part( 'templates/archives/archive-cpt' ); ?>
    <?php endwhile; endif; } ?>
    </div>
    

    The problem with it is that it shows all all of the destination post with the particular destination category ordered by state. It's spiffy, and getting very close to what I need but not exactly.

    #31571
    Long Nguyen
    Moderator

    Hi,

    For the query arguments

    $args = array(
        'post_type' => 'destination',
        'destination-category' => $destination->slug, // this is not an accepted argument
        'state' => $state->slug // this is not an accepted argument
    );

    They are not the accepted arguments destination-category and state. If you want to add the taxonomy to the query arguments, please follow this documentation
    https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

    See the lists of accepted arguments on the right side https://developer.wordpress.org/reference/classes/wp_query/#more-information

    #31601
    Rebecca Robertson
    Participant

    Okay, I've been over that page several times but it doesn't help with what I need.

    I need a page that shows the destination posts that are in both one destination category and one state.

    Similar to what is being done here How to Filter Posts by Custom Fields and Custom Taxonomies on Archive Pages but without the filter.

    #31612
    Long Nguyen
    Moderator

    Hi

    Here is the demo that queries posts from several taxonomies

    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'movie_genre',
                'field'    => 'slug',
                'terms'    => array( 'action', 'comedy' ),
            ),
            array(
                'taxonomy' => 'actor',
                'field'    => 'slug',
                'terms'    => 'abcd',
            ),
        ),
    );
    $query = new WP_Query( $args );

    If this still does not work for you, you can create a service request here https://metabox.io/contact/, our development team will help you with an extra fee.

    #31640
    Rebecca Robertson
    Participant

    Thank you. That code will work well enough, it's just more work than I was hoping to have to do as there are potentially 48 states that I would have to use that code for.

    I was hoping that I could dynamically call the state parks for each state like:

    if destination is in queried state and in queried destination category then show posts.

    something like this, except with a real argument where the term arguments go so that I don't have to explicitly name each state taxonomy:

    
    $args = array(
        'post_type' => 'destinations',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'destination-category',
                'field'    => 'slug',
                'terms'    => 'get_queried_object($destination-category->term);',//of course this needs to be a real argument
            ),
            array(
                'taxonomy' => 'state',
                'field'    => 'slug',
                'terms'    => 'get_queried_object($state->term);', //of course this needs to be a real argument 
            ),
        ),
    );
    $query = new WP_Query( $args );
    

    rather than having to call out each state individually with it's own argument set.

    As I said originally, it isn't about the code for calling posts from two taxonomies that I'm having trouble with, it's figuring out how to best achieve what I want, which is to dynamically call posts that share any one state taxonomy and any one destination taxonomy if and only if such a condition exists so that I don't have to write the code 48 separate times (1 call for every state in the lower 48).

    Is it better to use two taxonomies as I have, one taxonomy and a custom field, to somehow use relationships, or is there a bit of code that will allow me to not have to explicitly call each individual state and each individual destination category?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.