0

Ok, this may be a bit confusing but here goes...

Im working with several custom queries on the same page throughout my site. Some queries are grabbing regular post data, others are fetching CPTs depending on what page your on, namely the store section for example...

I had everything working perfect until I decided to try and make things easier for the client by converting an existing image gallery fader/slider into an "additional CPT" so that they could just add the images on their own when they want without having to continue requesting updates for this.

So in order, let me explain whats going on in the site (I'll also include a simple graphic to outline my situation):

  • I have a newly added CPT called featimggal to allow my clients to post their own images to the slider at the top of every page.

  • I have a CPT for my store section called product. I use a couple custom queries to grab different products based on a custom taxonomy slug and list them out on the landing page for the store.

  • I use a taxonomy template to show all product CPTs in an archive format when viewing items via taxonomy from links in the sidebar.

  • Lastly, I use a single-product.php template to display products by themselves.

.............

After adding a new CPT called featimggal with a custom query that comes before the product CPT's on the page everything still functions as expected when visiting the store.php page, however once you click an individual product to view it from the single-product.php template the cpt product does not appear there anymore and instead "all" the CPTs for the image gallery show up instead, its the strangest thing.

Examples:

Store Page - Featured Image Gallery CPT - featured_images.php

<?php $wp_query = new WP_Query(array('post_type' => 'featimggal')); ?>
<?php while ($wp_query->have_posts() ) : $wp_query->the_post(); ?>

<?php the_post_thumbnail('feat-img-gal', array('class' => 'featured_image_gallery', 'alt' => '', 'title' => '')); ?>

<?php endwhile; ?>
<?php wp_reset_query(); ?>

Store Page - Product CPT - store.php

<?php $wp_query = new WP_Query(array('post_type' => 'product', 'productcategory' => 'epr-apparel', 'posts_per_page' => 6, 'orderby' => 'rand', 'paged' => $paged )); ?>
<?php while ($wp_query->have_posts() ) : $wp_query->the_post(); ?>
// Do Stuff
<?php endwhile; ?>
<?php wp_reset_query(); ?>

<?php $wp_query = new WP_Query(array('post_type' => 'product', 'productcategory' => 'epr-accessories', 'posts_per_page' => 6, 'orderby' => 'rand', 'paged' => $paged )); ?>
<?php while ($wp_query->have_posts() ) : $wp_query->the_post(); ?>
// Do Stuff
<?php endwhile; ?>
<?php wp_reset_query(); ?>

Store Page - Footer Query - footer.php

<?php $wp_query = new WP_Query('category_name=blog&posts_per_page=1'); ?>
<?php while ($wp_query->have_posts() ) : $wp_query->the_post(); ?> 
// Do Stuff
<?php endwhile; ?>

store.php taxonomy.php single-product.php

1 Answer 1

1

Assign your custom queries to something other than $wp_query, that's the variable that holds the default query for the page and you're overwriting it with the image slider query.

5
  • Awesome, i will try that. It can be whatever I like correct? Also, should each custom query contain a different variable then, such as the one in the footer even? Thanks
    – Mr.Brown
    Commented Nov 6, 2011 at 2:45
  • Tried it out and it worked beautifully. Cant believe it was something that simple! Thanks again for the help Milo, you've proved to be a huge resource every time I come here.
    – Mr.Brown
    Commented Nov 6, 2011 at 3:54
  • Hey Milo, any idea why when I change wp_query to something unique in the index it decides to spit out a bunch of extra empty pages? I decided to go through all my custom queries like that and make them unique and they all work perfect, but when I change the one in the index template it creates this issue.
    – Mr.Brown
    Commented Nov 6, 2011 at 4:35
  • I'm not sure what the issue is you're describing. As for changing the footer query, you only have to worry about the ones that come before the use of the default query. Overwriting it after you've output all the posts the query contains is ok, though it may be less confusing to just make them all unique.
    – Milo
    Commented Nov 7, 2011 at 6:09
  • Right, thats what I thought too, so instead of just changing the one for the slider CPT I went and gave them all a unique name. By doing so they all still functioned as expected, however by changing the query I had written on the index.php template it decided to provide several empty page numbers below the posts for that query. Once I tried changing it back to just wp_query it removed the empty page numbers strangely enough.
    – Mr.Brown
    Commented Nov 7, 2011 at 15:02

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.