0

I didn't finde the right code for 3 days... now i'm asking you:

I want to achieve the following:

Here my post query:

$posts = new WP_Query(array(
    'post_type' => 'tarife',
    'posts_per_page' => 30,
    'post_status' => 'publish',
    'post__in' => $products
));

IF the term of my custom taxonomy[tarife] is 'info'

than show loop 1

while($posts->have_posts()):
    $posts->the_post();
    $response .= 'loop1';
endwhile;

IF the term of my custom taxonomy[tarife] is 'glasfaser'

than show loop 2

while($posts->have_posts()):
    $posts->the_post();
    $response .= 'loop2';
endwhile;

EDIT:

Now i have tried it like this but it shows me both loops if one of taxonomy exists.

$products = self::$productDefinitions[$result['Standorttyp']];
$posts_info = new WP_Query(array(
    'post_type' => 'tarife',
    'posts_per_page' => 30,
    'post_status' => 'publish',
    'post__in' => $products,
    'taxonomy' => 'info'
));

$posts_gf = new WP_Query(array(
    'post_type' => 'tarife',
    'posts_per_page' => 30,
    'post_status' => 'publish',
    'post__in' => $products,
    'taxonomy' => 'glasfaser'
));


if ( $posts_info->have_posts() ) :
    while ( $posts_info->have_posts() ) : $posts_info->the_post();    
        $response .= 'infos';
    endwhile;
    wp_reset_postdata(); 
endif;

if ( $posts_gf->have_posts() ) :
    while ( $posts_gf->have_posts() ) : $posts_gf->the_post();    
        $response .= 'tarife';
    endwhile;
    wp_reset_postdata(); 
endif;

If I choose option 1 in the frontend, the result should be: tarife tarife tarife

If I choose option 2 in the frontend, the result should be: info

What i get:

option 1: info info info tarife tarife tarife

option 2: info tarife

1
  • You can set taxonmies and terms in WP_Query, not sure if I understand your question.
    – cybmeta
    May 30, 2018 at 6:49

1 Answer 1

0

Have found the solution

I only have to add the right tax_query...

    $posts_info = new WP_Query(array(
        'post_type' => 'tarife',
        'posts_per_page' => 3,
        'post_status' => 'publish',
        'post__in' => $products,
        'orderby' => 'title',
        'tax_query' => array(
            array(
                'taxonomy' => 'tarif_kategorie',
                'field' => 'slug',
                'terms' => 'info',
            ),
        ),
    ));

    $posts_gf = new WP_Query(array(
        'post_type' => 'tarife',
        'posts_per_page' => 5,
        'post_status' => 'publish',
        'post__in' => $products,
        'orderby' => 'title',
        'order' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'tarif_kategorie',
                'field' => 'slug',
                'terms' => 'glasfaser',
            ),
        ),
    ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.