0

I'm attempting to loop thought a custom post type (artists), then each category, then another custom post type (story) which has a relationship with the artist.

The issue is, the function ID ); ?> in the final loop is returning a title of the Artist many times over, along with the title of the current custom post. I just need the title of the current loops post.

    <!-- get all the artists -->

 <?php

  $args = array('post_type' => 'artists');

    $query = new WP_QUERY( $args );

    ?>

    <?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>

        <h1><?php the_title(); ?></h1>

        <p><?php the_field('artist_website'); ?></p> 

    <!--  looping through all the categories -->
            <?php

            $cats = get_categories(); 

                // loop through the categries
                foreach ($cats as $cat) {
                    // setup the cateogory ID
                    $cat_id= $cat->term_id;
                    // Make a header for the cateogry
                    echo "<h2>".$cat->name."</h2>";
                    ?>

                 <!--  looping through the stories that have a relationship with the artist (select_artist) -->
                    <?php
                        $post_type = 'story';

                    $args = array(
                            'post_type' => $post_type,
                            'posts_per_page' => -1,  //show all posts
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'category',
                                    'field' => 'slug',
                                    'terms' => $cat->slug,
                                )
                            ),
                            'meta_query' => array(
                                array(
                                    'key' => 'select_artist', // name of custom field
                                    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123.
                                    'compare' => 'LIKE'
                                )
                            )
                        );

                    $stories = new WP_Query($args);

                    ?>

                    <?php if( $stories ): ?>

                        <ul>
                        <?php foreach( $stories as $story ): ?>
                           <!-- Problem echo below -->
                            <?php echo get_the_title( $story->ID ); ?> 

                        <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>


                <?php } // done the foreach statement ?>

        <?php endwhile; wp_reset_postdata(); endif; ?>  


**EDIT** var dump of $stories

object(WP_Query)#307 (46) { ["query"]=> array(4) { ["post_type"]=> string(5) "story" ["posts_per_page"]=> int(-1) ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["field"]=> string(4) "slug" ["terms"]=> string(12) "category-one" } } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(13) "select_artist" ["value"]=> string(5) ""104"" ["compare"]=> string(4) "LIKE" } } } ["query_vars"]=> array(63) { ["post_type"]=> string(5) "story" ["posts_per_page"]=> int(-1) ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["field"]=> string(4) "slug" ["terms"]=> string(12) "category-one" } } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(13) "select_artist" ["value"]=> string(5) ""104"" ["compare"]=> string(4) "LIKE" } } ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(12) "category-one" ["tag"]=> string(0) "" ["cat"]=> int(6) ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["nopaging"]=> bool(true) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["order"]=> string(4) "DESC" } ["tax_query"]=> object(WP_Tax_Query)#304 (6) { ["queries"]=> array(1) { [0]=> array(5) { ["taxonomy"]=> string(8) "category" ["terms"]=> array(1) { [0]=> string(12) "category-one" } ["field"]=> string(4) "slug" ["operator"]=> string(2) "IN" ["include_children"]=> bool(true) } } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(1) { [0]=> string(21) "wp_term_relationships" } ["queried_terms"]=> array(1) { ["category"]=> array(2) { ["terms"]=> array(1) { [0]=> string(12) "category-one" } ["field"]=> string(4) "slug" } } ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" } ["meta_query"]=> object(WP_Meta_Query)#305 (7) { ["queries"]=> array(2) { [0]=> array(3) { ["key"]=> string(13) "select_artist" ["value"]=> string(5) ""104"" ["compare"]=> string(4) "LIKE" } ["relation"]=> string(2) "OR" } ["relation"]=> string(3) "AND" ["meta_table"]=> string(11) "wp_postmeta" ["meta_id_column"]=> string(7) "post_id" ["primary_table"]=> string(8) "wp_posts" ["primary_id_column"]=> string(2) "ID" ["table_aliases":protected]=> array(1) { [0]=> string(11) "wp_postmeta" } } ["date_query"]=> bool(false) ["request"]=> string(525) "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) ) AND wp_posts.post_type = 'story' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( ( wp_postmeta.meta_key = 'select_artist' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%\"104\"%' ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC " ["posts"]=> array(0) { } ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(true) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(true) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "6db7f7961e185d6e6411219e7317dd72" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL }
4

The issue, I believe, is that if ($stories) is always returning true, een if there are no posts. It's then looping through the WP-Query elements, not the actual posts. Try this instead:

if ( $stories->have_posts() ) {
  while ( $stories->have_posts() ) : $stories->the_post();

     echo get_the_title( get_the_ID() );

  endwhile; $query->reset_postdata();
}

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.