0
votes

I am trying to query Wordpress custom posts and categories they are related to. Query acts strangely, displays category name and all customs posts, not even related to that category (keeps repeating till all categorie names are displayed.)

Example image : Wordpress custom query display

My query code:

<div id="page-content-wrapper">
    <div class="container-fluid">
        <div class="lookbook-header">
            <div class="wrap">
                <p class="text-left">lookbook</p>
            </div>
        </div>

        <?php
        $taxonomy = 'lookbook_categories';
$terms = get_terms($taxonomy);

    $args=array(
     'taxonomy' => 'lookbook_categories'
    'post_type' => 'lookbook',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
        if ( $terms && !is_wp_error( $terms ) ) :
        foreach ( $terms as $term ) { ?>
            <div class="lookbook-category">
                <p class="text-center">
                    <?php echo $term->name; ?>
                </p>
            </div>
            <?php
if( $my_query->have_posts() ) {
        echo '';
        $count=0; 
while ($my_query->have_posts()) : $my_query->the_post();
if($count == 3) {?>
                <div class="row">
                    <?php }
    ?>
                        <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 no-padding">
                            <div class="lookbook-item">
                                <div class="hvrbox">
                                    <?php 
                                    $image = get_field('lookbook_image');
                                    if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="news" class="img-responsive" />
                                        <?php endif; ?>
                                            <div class="hvrbox-layer_top">
                                                <div class="hvrbox-text">
                                                    <div class="separator"></div>
                                                    <h3><?php the_title();?></h3>
                                                    <div class="separator"></div>
                                                    <p>
                                                        <?php the_field('excerpt');?>
                                                    </p>
                                                </div>
                                            </div>

                                </div>

                            </div>
                        </div>
                        <?php    
   $count++; 
        if($count == 3) echo '</div>';
        endwhile;
}
                                      }
                    endif;
wp_reset_query();
?>

                </div>
    </div>

I would like to display category name + posts related to category (not all posts over and over again)

1

1 Answers

0
votes

You want to pass in an array to get_terms like:

$terms = get_terms( array(
  'taxonomy' => 'lookbook_categories',
  'hide_empty' => false,
) );

you can read more about it here: https://developer.wordpress.org/reference/functions/get_terms/