0

I have onnly VERY LIMITED knowledge in programming and coding.

On our website (travel agency) we offer different sample-trips sorted by regions (Mexico, Middle-America, Southamerica and other countries), see here: Neue Welt Reisen/trips

When you select a trip e.g. Costa Rica you will find other sample trips at the bottom of the page. These trips should be selected from the same region, in this case Middle-America and not from the 3 other regions. There is a script that should this, but the results are only trips from the region Mexico. So my question is: How do I select the example-trips from the same region? Here is the code:

<div class="promo-trips-container">
    <?php
    $args = array(
        'post_type'     => 'ausfluge',
        'showposts'     => 3,
        'orderby'       => 'rand',
        'tax_query'     => array(
            array(
                'taxonomy' => 'reisezweck',
                'field'    => 'slug',
                'terms'    => array( 'mexiko', 'middle-america', 'south-america', 'other' ),
            ),
        ),
    );
    $trips_list = new WP_Query( $args );
    ?>

THANK YOU

Paul

0

so if $trips_list is the sample trips section, and you want the sample trips to only show trips with the same terms - you'd find the current trips term and argue that in your WP_Query.

// this trips terms
$tripTerms = wp_get_post_terms( get_the_ID(), 'reisezweck');

// if each trip only has one term (as its a location)
// we'll use the first of the array array of terms
// and we'll now have our current trips term
$tripTerm = $tripTerms[0]->slug;

// you can uncomment and use instead if it's possible a trip has multiple terms: 
// make an array of ID of current trips terms
/* $tripTerm = array();
foreach ($tripTerms as $aTripTerms) 
    $tripTerm[] = $aTripTerms->slug; */

$args = array(
    'post_type'     => 'ausfluge',
    'showposts'     => 3,
    'orderby'       => 'rand',
    'tax_query'     => array(
        array(
            'taxonomy' => 'reisezweck',
            'field'    => 'slug',
            'terms'    => $tripTerm,
        ),
    ),
);
$trips_list = new WP_Query( $args );

I've assumed each trip would have only one term. I've added commented code incase a trip could have multiple.

1
  • 1
    Dear David, Thank you very much, that was exactly what I wanted to know. It works fine. Have a nice day. Paul Dec 12 '17 at 12:15

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.