Can't show custom taxonomy in WordPress WP_Query - php

I have Custom Post Types in my website, and a Custom Taxonomies inside of each.
When i use just 'post_type' in WP_Query array with a CPT name in it, it works fine, but when i want to display childrens (taxonomies) i have nothing in response.
Example structure (let's say that there are registered custom names):
Custom_Post1
|--- Custom_Tax1
|--- Custom_Tax2
Custom_Post2
|--- Custom_Tax1
|--- Custom_Tax2
Code which works GOOD:
$current_items_args = array(
'post_type' => 'Custom_Post1',
);
$current_items = new WP_Query( $current_items_args );
<?php if ($current_items->have_posts()): ?>
<?php while ($current_items->have_posts()): ?>
<?php $current_items->the_post(); ?>
<h2><?php the_title();?></h2>
<?php endwhile; ?>
<?php endif; ?>
Code which works BAD:
$current_items_args = array(
'tax_query' => array(
array(
'taxonomy' => 'Custom_Tax1',
'field' => 'slug',
),
),
);
$current_items = new WP_Query( $current_items_args );
<?php if ($current_items->have_posts()): ?>
<?php while ($current_items->have_posts()): ?>
<?php $current_items->the_post(); ?>
<h2><?php the_title();?></h2>
<?php endwhile; ?>
<?php endif; ?>
I have tryed many similar posts in stackoverflow and entire google, but can't understand where is the problem. Already spent on it all day. It is possible that i put some bad collons right here but that's not the point in my problem.
Thanks

Related

wordpress php loop - archive is looping the same number of times as results

Not sure that's the best way to to describe it, but here's what's happening (and I'm sure this has to be a reasonably easy thing, just not sure where to look):
Client has 10 offices so I'm using the Advanced Custom Fields plugin with a custom post type to specify a location for job postings. My code seems to be working (i.e. it's pulling the appropriate jobs by location) but it's looping the article tag the same number of times as results. I have 3 jobs in there, so it loops the whole set of results 3 times. If I delete one and drop down to 2, it loops twice, etc. I used the following example from the advanced custom fields website:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I guess I'm not sure where to look (I realize this might be a plugin question rather than a php question) and would appreciate any direction you guys can provide.
You can see the offending page here: http://www.knrlegal.com/jobs/
You should reset the post data, not the query, as stated here:
Class Reference/WP Query, in the "Multiple Loops" section.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
?>
<?php endif; ?>

Why is next_post_link() not working with WP_Query?

Been researching a pagination issue I am having with WordPress. My page-foobar.php file is:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$foo_query = new WP_Query( $foobar_args );
if ( $foo_query->have_posts() ) : ?>
<?php while ( $foo_query->have_posts() ) : $foo_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
I added:
<nav>
<?php previous_posts_link('« Newer',$foo_query->max_num_pages); ?>
<?php next_posts_link('Older »',$foo_query->max_num_pages); ?>
</nav>
after referencing this answer after the endif;:
The link shows in the url as site/foobar/page/2/ but displays the 404 page. After some further research I ran across an article that mentioned the page-foobar.php and posttype-foobar.php names can clash so to rename one of them. I renamed posttype-foobar2.php, changed my functions.php, tested and still get sent to the 404 page with the correct URL. Another article mentioned that you must use wp_query() so I changed my page-foobar.php to:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
pagination:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
I did test this by adding this before <?php wp_reset_postdata(); ?> and after <?php endwhile; ?>:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
tested and I still get sent to a 404 page. When I change next_posts_link() to <?php var_dump(next_posts_link('« Older Entries', $wp_query->max_num_pages)); ?> it returns NULL. If I change next_posts_link() to get_next_posts_link() I get string(99). What am I doing wrong with my pagination?
Further Referenced:
Wordpress pagination (next_posts_link) on custom wp_query not showing
next_posts_link 404 error
WP_Query and next_posts_link
How can I properly get my pagination to go to the next page?
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
After doing some further research I ran across the solution to my problem and I hope it can help someone else. The answer submitted by Sindhu does have merit in respect to the addition of:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
and the modification to the arguments:
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
Codex for get_query_var(). After making this additional change it still didn't work so I started testing my Permalinks. When I ran the code under Default the pagination worked. When I changed to Post name and saved twice it still didn't work. After browsing I decided to search for custom post type giving 404 error and I ran across "Fixing Custom Post Type 404 Errors In WordPress" which mentioned I could be creating a double slug. After review I was in fact doing so and WordPress was clashing with the double slug. I decided to changed my posttype-foobar.php to posttype-foobar_t.php and updated my functions.php file. Instead of modifying the database on my localhost to fix the slug issue I decided to install a clean installation with the modified changes and it works.
I would also like to point out some of my findings in regards to a large majority of posts mentioned you have to call the query as $wp_query = new WP_Query( $foobar_args );.

Wordpress Custom Loop doesn't show any posts

I'm trying to add a custom loop to display post titles from the category homepage. Here's the code I have so far so, but it's not displaying any posts. BTW, I'm using this code in single.php.
<?php $recentPosts = new WP_Query(); ?>
<?php if ( $recentPosts->have_posts() ) : ?>
<?php $recentPosts->query_posts( array ( 'category_name' => 'homepage', 'posts_per_page' => 10 ) ); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I'm not too sure what's going on, but would appreciate any help I can get.
You're doing a lot of unnecessary messing around with your query object. Since you're dealing with an archive page, you should be able to create a new template file called category-homepage.php (assuming the slug of the category is homepage). In it, you could place something like the following:
$args = array(
'category_name' => 'homepage',
'posts_per_page' => 10
);
$recentPosts = new WP_Query( $args );
if ( $recentPosts->have_posts() ) :
while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Again, since it's a category archive page, you shouldn't be doing anything to single.php. You can read more about template hierarchy in the Codex.

Wordpress Query by Tag(Priority) then Category(backfill)

I've search high and low but have been unable to find a solution to my problem. Hopefully this isn't a duplicate question that I was unable to find through search here and on google.
I'm attempting to have a wp_query return a set number of results (10) that are populated by any posts found in the current pages category. I was able to accomplish this with...
$postCategories = get_the_category();
$atts = array (
'posts_per_page' => 10,
'tag' => 'sticky',
'category_name' => $postCategories[0]->slug,
);
But where I'm having trouble is with the tag. I would like any posts that have the tag 'sticky' to take priority over any of the posts being brought in by the category match while not adding any more than 10 results still.
Any help or guidance would be much appreciated as i'm kind of a newbie to php. Thanks
I think this could work for you, but it's hard to know for sure without knowing the specifics of your project.
<ul>
<?php $sticky = get_option( 'sticky_posts' ); // Get sticky posts ?>
<?php $args_sticky = array(
'post__in' => $sticky,
'posts_per_page' => 10, // Limit to 10 posts
'ignore_sticky_posts' => 1
); ?>
<?php $sticky_query = new WP_Query( $args_sticky ); ?>
<?php $sticky_count = count($sticky); // Set variable to the number of sticky posts found ?>
<?php $remaining_posts = 10 - count($sticky); // Determine how many more non-sticky posts you should retrieve ?>
<?php if ($sticky_count > 0) : // If there are any sticky posts display them ?>
<?php while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php if ($remaining_posts > 0) : // If there are non-sticky posts to be displayed loop through them ?>
<?php $postCategories = get_the_category(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => $remaining_posts,
'post__not_in' => get_option( 'sticky_posts' ),
'category_name' => $postCategories[0]->slug
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php endif; ?>
</ul>

ACF custom post feed by custom taxonomy

I'm using wordpress with custom post type UI plugin and ACF plugin.
Trying to build a “single” template with multiple feeds of custom post types by custom custom taxonomy. Using this code, with a few variations to figure out what am i doing wrong.
Got 2 pieces of code like this in a row
<?php if( get_field('collectiona') ):
$argsc = array(
'post_type' => 'products',
'product-collections' => get_field('collectiona'),
);
$prods2 = new WP_Query( $argsc );
if( $prods2->have_posts() ) {
while( $prods2->have_posts() ) {
$prods2->the_post();
?>
Whatever post code
<?php
}
}
else {
echo '';
}
?>
<?php endif; ?>
collectiona is a taxonomy field. With the piece of code, shown above, it just shows all the “products” posts out there. I’ve also tried using a text field with taxonomy slug. It shows first feed perfectly fine, if i’m not using first if statement (<?php if( get_field(‘collectiona’) ): ?>), and if that statement is present- same thing happens. All the “products” are shown. However, even with first feed shown fine, 2nd feed still shows all the “products” out there. Despite what taxonomy slug says.
I’m trying to build it the way, admin could chose a dropdown taxonomy. Text field with taxonomy slug is just an example.
p.s.
I use term object
Full template code is here jsfiddle.net/pudfbxhv . I know jsfiddle is useless for wp templates, but that's a pretty big piece of code
EDIT
Here is updated code
<?php
$taxterms = get_field("collectiona"); ?>
<?php
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-collections',
'field' => 'id',
'terms' => $taxterm->term_id
)
)
);
$myquery = new WP_Query( $args );
if($myquery->have_posts()) : ?>
<ul>
<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<li> <img src="<?php the_field('prod_featured_image'); ?>" onmouseover="this.src='<?php the_field('prod_hover_featured_image'); ?>'" onmouseout="this.src='<?php the_field('prod_featured_image'); ?>'" />
<h2><?php the_field('prod_subtitle'); ?></h2>
<p>$<?php the_field('prod_price'); ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
Well, that may be a perversion, but it worked for me.
$termss = get_field('collectiona');
$slll = $termss->slug;
$args = array(
'post_type' => 'products',
'product-collections' => $slll,
);
$lineblocks = new WP_Query( $args );
if( $lineblocks->have_posts() ) {
while( $lineblocks->have_posts() ) {
$lineblocks->the_post();
Also, gotta remember to put the following code after every array
<?php wp_reset_query(); ?>

Categories