0

Why is this code not retrieving the posts of custom post type "graphic-design"?

<?php
    $args = array( 'pagename' => 'graphic-design' );
    $query = new WP_Query($args);
    while ( $query->have_posts() ) : $query->the_post();
    ?>

        <div id="graphic-design" class="active page clearfix">

            <h3 class="subtitle"><?php the_title(); ?></h3>

            <?php
            $inner_args = array( 'post_type' => 'graphic-design' );
            $inner_query = new WP_Query( $inner_args );
            while ( $inner_query->have_posts() ) : $inner_query->the_post();
            ?>

                <div class="work">
                    <?php the_post_thumbnail(); ?>
                </div>

            <?php 
            endwhile;
            wp_reset_postdata();
            ?>

        </div>

    <?php 
    endwhile;
    wp_reset_postdata();
    ?>

When I run the inner query on its own, outside of the outer query it works fine.

  • 1
    does the outer query work without the inner query? – Milo Dec 2 '13 at 19:20
  • Yes the outer query works fine with or without the inner one – Danz0r77 Dec 2 '13 at 19:28
  • Where are you putting this query? As in, are you running it in page.php or some kind of template file? Are you getting any kind of error or what do you mean by 'not working'? – Howdy_McGee Dec 2 '13 at 19:35
  • I'm running it in page.php. EDIT: There are no errors - the h3 tag in the outer query is displayed but the inner div does not display. When the inner query is moved outside the outer query it displays fine. – Danz0r77 Dec 2 '13 at 20:00
  • Is this code in full or simplified? What's the point of outer query at all? Query for one page, which is always same? – Rarst Dec 2 '13 at 20:56
0

Have you checked slug of 'graphic-design' CPT?. Maybe there are conflicts between page slug and CPT slug?

You can't really do what you are trying to do in the way you would like to do it.

/graphic-design/ is registered with the custom post type as the custom post type permalink associated with that custom post type.

when you give a page the same permalink that is associated with the custom post type permalink, you create a conflict. WordPress knows how to display pages based on the requested URL and what are called the rewrite rules. This is a sequential list of all the possible rules of URL's that your site can display. It goes down the list one by one looking for a rule that matches. Custom Post Type rules are ahead of page rules in the sequence. So when WordPress sees site.com/portfolio/ it knows that as the custom post type archive page, and that is what its going to display. It doesn't even reach the part of its rules that are aware of site.com/graphic-design/ as a page because it has already taken action on the first match it found, which is the custom post type archive page. You have a URL clash - two types of content trying to display at the same URL. That won't work.

Answer from WordPress forum read more

  • Thanks. I'm not sure this is the problem though. I didn't mention it before but I originally had the custom post type named as "work" then changed it to "graphic design" as part of trying to fix the problem. I'm not sure if the permalink clash would affect this anyway, would it? The page might not load but the query should still work I would've thought. – Danz0r77 Dec 2 '13 at 20:09

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.