-
AuthorPosts
-
June 26, 2015 at 6:29 pm #314136
Hi hi!
I’m trying to use wp_query on a custom post type archive page to only loop in two terms from said custom post type’s taxonomy, and can’t seem to get it right (unless there is something else going on that I’m missing).
I have created a custom archive-{cpt}.php, wp-archive-{cpt}.php, and _archive-{cpt}.php and connected them, confirming that they do actually affect the archive’s display, and then attempted to wp_query the loop to call only the taxonomy terms I want using tax_query. I am new to wp_query but thought I had it right — unfortunately, I keep getting all posts instead of the posts from the two taxonomy terms. I tried several different ways, but received either errors or a blank page with pagination numbers at the bottom.
Here is the base of what I tried on the _archive-{cpt}.php file:
<div id="x-iso-container" class="x-iso-container x-iso-container-posts cols-<?php echo $cols; ?>"> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php if ( $stack != 'ethos' ) : ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php else : ?> <?php x_ethos_entry_cover( 'main-content' ); ?> <?php endif; ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; ?> <?php wp_reset_postdata(); $args = array( 'post_type' => 'customposttype', 'tax_query' => array( array( 'taxonomy' => 'cpt_category', 'field' => 'slug', 'terms' => 'category-term-1', 'category-term-2', ), ), 'posts_per_page' => 10 ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php if ( $stack != 'ethos' ) : ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php else : ?> <?php x_ethos_entry_cover( 'main-content' ); ?> <?php endif; ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; ?> </div> <?php pagenavi(); ?>
Note that I tried paring down/removing the if statements, calling the query in various areas, etc. Hopefully, you can point me in the right direction.
Thanks!
June 27, 2015 at 12:37 am #314351Hello There,
Thanks for writing in!
To assist you with this issue, we’ll first need you to provide us with your URL with ftp and login credentials so we can take a closer look. We want to see how you organize your template files.
To do this, please post
– Link to your site
– WordPress Admin username / password
– FTP user / passwordDon’t forget to select Set as private reply so that it is only visible to our staff.
This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you with everything.
Thank you.
June 29, 2015 at 11:32 am #316143This reply has been marked as private.June 29, 2015 at 12:16 pm #316175Hey There,
Please update your $args variable a bit:
$args = array( 'post_type' => 'customposttype', 'tax_query' => array( array( 'taxonomy' => 'cpt_category', 'field' => 'slug', 'terms' => array('category-term-1', 'category-term-2') ), ), 'posts_per_page' => 10 );
Hope it helps.
June 29, 2015 at 1:47 pm #316265This reply has been marked as private.June 30, 2015 at 6:47 am #316896Hi there,
Thanks for writing in!
This is more likely a custom development, that is outside the scope of support we can offer. However, I could point you in the right direction with the understanding that it would ultimately be your responsibility to take it from here.
I’m unable to login to your FTP account using the given credentials. Please confirm again. For now, can you reverse the change you made so I can see the posts/listings and check the causing conflict?
Thanks!
June 30, 2015 at 8:21 am #316975This reply has been marked as private.June 30, 2015 at 11:39 am #317214Hi there,
Thanks for the correct logins!
I just went ahead and updated your code with following, this should now work just fine:
<?php // ============================================================================= // VIEWS/GLOBAL/_INDEX.PHP // ----------------------------------------------------------------------------- // Includes the index output. // ============================================================================= $stack = x_get_stack(); if ( is_home() ) : $style = x_get_option( 'x_blog_style', 'standard' ); $cols = x_get_option( 'x_blog_masonry_columns', '2' ); $condition = is_home() && $style == 'masonry'; elseif ( is_archive() ) : $style = x_get_option( 'x_archive_style', 'standard' ); $cols = x_get_option( 'x_archive_masonry_columns', '2' ); $condition = is_archive() && $style == 'masonry'; elseif ( is_search() ) : $condition = false; endif; // ============================================================================= // INSTRUCTIONS ADDED BY X STAFF // ============================================================================= // // Added this line so the condition will always be true // this will enable the masonry style // set it to false if you don't want Masonry layout // $condition = true; // // Change "2" to your desired value (3, 4) to change the column // count on this custom archive page // $cols = 2; // // Adding the WP_Query $args // $args = array( 'post_type' => 'bullhornjoblisting', 'tax_query' => array( array( 'taxonomy' => 'bullhorn_category', 'field' => 'slug', 'terms' => array('accounting-finance-executive-and-professional', 'accounting-finance-term-and-permanent') ), ), 'posts_per_page' => 10 ); ?> <div class="x-container max width"> <?php if ( $condition ) : ?> <?php x_get_view( 'global', '_script', 'isotope-index' ); ?> <div id="x-iso-container" class="x-iso-container x-iso-container-posts cols-<?php echo $cols; ?>"> <?php $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php if ( $stack != 'ethos' ) : ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php else : ?> <?php x_ethos_entry_cover( 'main-content' ); ?> <?php endif; ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; wp_reset_postdata(); ?> </div> <?php else : ?> <?php $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : the_post(); ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; ?> <?php endif; ?> <?php pagenavi(); ?> </div>
You can compare both using a tool difference tool such as https://www.diffchecker.com/. I’ve also added some instructions at the top of the page, you may also want to review them.
Thanks!
June 30, 2015 at 12:39 pm #317279You guys are -AWESOME-!
It works swimmingly — with one small hiccup: the pagenavi code at the bottom gave me 8 pages of the same thing over and over again. I solved that by simply deleting it and changing the posts per page to -1, but wouldn’t mind knowing why it happened inn case I get asked to add pagination back in.
June 30, 2015 at 10:36 pm #317750Hi there,
-1 means select all, that could be the reason why you’re getting same pages since you’re selecting same results. But it’s a little weird too, and glad it’s solved now.
Thanks!
July 1, 2015 at 12:57 pm #318504It was 10 before and caused the problem. I changed it to -1 to fix it. If there was a lot of content it would be a problem, though.
July 1, 2015 at 5:38 pm #318748Hi there,
Ah, I see what’s missing. The query_var for paging. You should utilize both query_var[‘page’] and posts per page if you wish your posts to be paged correctly.
Change this code :
$args = array( 'post_type' => 'bullhornjoblisting', 'tax_query' => array( array( 'taxonomy' => 'bullhorn_category', 'field' => 'slug', 'terms' => array('accounting-finance-executive-and-professional', 'accounting-finance-term-and-permanent') ), ), 'posts_per_page' => 10 );
to this :
$paged = ( is_front_page() ) ? get_query_var( 'page' ) : ( ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1 ); $args = array( 'post_type' => 'bullhornjoblisting', 'tax_query' => array( array( 'taxonomy' => 'bullhorn_category', 'field' => 'slug', 'terms' => array('accounting-finance-executive-and-professional', 'accounting-finance-term-and-permanent') ), ), 'posts_per_page' => 10, 'paged' => $paged );
Thanks!
July 1, 2015 at 8:28 pm #318846Thanks again! That’s nearly there, but it still shows that there are 8 pages of content when there are only 2. I’m guessing that page_navi is trying to account for the content in the other categories that were stripped out by the custom query.
Is there a way to make page_navi only correspond to the terms in the custom query?
You have been awesome, by the way! I had been smashing my face into this for a while before I asked for help. I really appreciate it, especially considering that I am extending the theme into custom design land a bit. 🙂
July 2, 2015 at 3:20 am #319089Hi,
I was able to fix this by changing $the_query to $wp_query.
Kindly check on your end.
Thanks
July 2, 2015 at 9:50 am #319301Beautiful! You are an awesome person and I might well kiss you if you were reachable! 😀
X and X support are the best!
-
AuthorPosts