0

I want to create a loop based on custom post types and taxonomies. But i don't really understand the WP_query and loop.

I have a custom post type called "hanstholm" with a custom taxonomy called "kategori". In that i have "tilbud" and several others. I've tried to create a loop with this:

<?php $query = new WP_Query( array(
'post_type' => 'Hanstholm',          // name of post type.
'tax_query' => array(
    array(
        'taxonomy' => 'kategori',   // taxonomy name
        'field' => 'kategori',           // term_id, slug or name
        'terms' => 'tilbud',                  // term id, term slug or term name
    )
) ) );
while ( $query->have_posts() ) : $query->the_post();
?> 
<section class="section">
    <div class="container-wrap">
        <div class="container">
                <div class="row" style="padding-bottom:0">


                                                <div id="artikel-normal">
                                                    <div id="artikel-image-left" class="col-sm-12 col-md-12 col-lg-6" style="background:linear-gradient( rgba(0, 0, 0, 0.0), rgba(0, 0, 0, 0.1)), url(<?php the_field( 'intro_billede' ); ?>); background-position: 50% 50%; background-size: cover;">
                                                    </div>
                                                    <div id="artikel-content-right" class="col-sm-12 col-md-12 col-lg-6">
                                                        <div class="hotel-name-artikel">
                                                        <?php the_field( 'intro_lille_overskrift' ); ?>
                                                    </div>
                                                    <div class="forside-artikel-overskrift">
                                                        <h2><?php the_title(); ?></h2>
                                                    </div>
                                                    <div class="forside-artikel-intro">
                                                        <?php the_field( 'intro_tekst' ); ?>
                                                    </div>
                                                            <?php $intro_button_1 = get_field( 'intro_button_1' ); ?>
                                                            <?php if ( $intro_button_1 ) { ?>
                                                        <div class="forside-artikel-link">
                                                                <div><a class="button--tertiary" style="float:left" href="<?php echo $intro_button_1; ?>"><?php the_field( 'intro_button_1_tekst' ); ?></a></div>
                                                            <?php } ?>

                                                        <?php $intro_button_2 = get_field( 'intro_button_2' ); ?>
                                                        <?php if ( $intro_button_2 ) { ?>
                                                            <div><a class="button--quaternary" style="float:left" href="<?php echo $intro_button_2; ?>"><?php the_field( 'intro_button_2_tekst' ); ?></a></div>
                                                        <?php } ?>
                                                    </div>
                                                    </div>
                                                </div>






</div>

What am i doing wrong here? It displays nothing.

1
  • first your post type is capitalized. Are you sure that's how it was created? Secondly, try 'field' => 'slug' instead of 'field' => 'kategori'
    – rudtek
    Jan 29, 2020 at 17:12

1 Answer 1

0

Maybe the hanstholm in $args without the uppercase ?

Also a problem with tax_query

Don't forget to use var_dump($query) for see what hapening.

<?php $query = new WP_Query( array(
'post_type' => 'hanstholm',          // name of post type.
'tax_query' => array(
    array(
        'taxonomy' => 'kategori',   // taxonomy name
        'field' => 'name',           // term_id, slug or name
        'terms' => 'tilbud',                  // term id, term slug or term name
    )
) ) );
while ( $query->have_posts() ) : $query->the_post();
?> 
2
  • 1
    Not sure why this is downvoted. Seems like a good answer.
    – rudtek
    Jan 29, 2020 at 17:14
  • no idea, i didn't downvote Jan 30, 2020 at 8:58

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.