hey i'm trying to display posts that share term from two different taxonomies, but for some reason i just displays all posts. Not sure how to get this working. any help would be awesome! below is the arguments im using for my wp_query.
thanks in advance.
$args = array(
'post_type' => $posttype,
'posts_per_page' => 99999,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'mytag',
'field' => 'slug',
'terms' => 'tag1',
),
array(
'taxonomy' => 'mycategory',
'field' => 'slug',
'terms' => 'cat1',
),
),
);
html
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?>
<?php endif;?>
html here...
<?php endwhile; ?>
<?php else: ?>
<h2>No posts found</h2>
<?php endif;
die();
// Correct code you closed endif before endwhile
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?>
html here...
<?php endwhile; ?>
<?php else: ?>
<h2>No posts found</h2>
<?php endif;
die();
Related
I am trying to exclude a category from a custom post type loop but with no luck. I have no idea why this isn't working. I have looked all over the internet but no luck, please can someone help identify the issue.
I have a category with an ID of 141.
I am running through a loop of posts where I am trying to exclude posts assigned to that category but they are still showing up in the loop. Where am I going wrong? Here is my code.
<?php
$args = array(
'post_type' => 'programme',
'cat' => -141,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="grid-item">
<?php the_title(); ?>
</div>
<?php
endwhile;
else:
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
wp_reset_postdata();
?>
For anyone with a similar issue, I managed to fix it by using this code instead.
$args = array(
'post_per_page' => '-1',
'post_type' => 'programme',
'tax_query' => array(
array(
'taxonomy' => 'ticket-category',
'field' => 'slug',
'terms' => array( 'savers-tickets' ),
'operator' => 'NOT IN',
)
),
);
Im trying to display specific product categories in an ACF repeater field.
This keeps outputting all product categories.
How do I go to output single category per repeater field?
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_category_ids = get_sub_field('project_category');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'terms' => $product_category_ids
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
<?php endwhile; ?>
</ul>
This worked for me:
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_cats = get_sub_field('product_category');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_cats,
'operator' => 'IN',
'field' => 'slug'
),
)
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endwhile; ?>
</ul>
I have been trying to list my custom type 'movies' with the custom taxonomy genre with a value of drama.
I have tried several solutions but as yet I have failed.
<?php
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'term_id',
'terms' => 'drama'
)
)
);
$drama = new WP_Query( $args );
if ( $drama->have_posts() ) : while ( $drama->have_posts() ) : $drama->the_post(); ?>
<h4><?php echo the_title(); ?></h4>
<?php endwhile; ?>
<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hope someone can shed some light on this matter.
Steven
The field parameter is wrong, in your case you need to set it to slug as drama is not a term_id
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'drama'
)
)
);
Hope it works !
I have created a custom taxonomy for a custom post. Now i want to show all post related to a particular term. Suppose i have two terms term1 and term2. When click on term1 then all post related to term1 will show and post , that is related to term2 will not show. But now when i click term1 then post related to term1 and term2 are showing at a time. I have wrote the following code to taxonomy.php template.
<div class="main-content">
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->name,
),
) );
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
<?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' ); ?>
<hr />
</div>
<?php
endwhile;
wp_reset_postdata();
else:?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php
endif;
?>
</div>
Please tell me , how can i achieve that.
I have got the answer .
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
You should use slug instead of name like following code:-
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
Hope this will help you.
Following is my wordpress query which I am using to display all the posts but the query is not displaying the post's tags, Also kindly let me know how to modify the following query so it displays the posts from any specific category.
<?php
$wp_query2 = null;
$wp_query2 = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'caller_get_posts'=> 0 ));
while ($wp_query2->have_posts()) : $wp_query2->the_post();
?>
<?php the_date(); ?>
<br />
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
wp_reset_query();
?>
You may try this
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'category1', 'category2' ) // replace these
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array( 'tag1, tag2' ) // replace these
)
)
);
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
// ...
the_content();
the_tags(); // display tags
endwhile;
Check WP Query and the_tags.