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>
Related
I can't seem to work out why my ACF fields will not display below my woo-commerce products on the featured page when they display fine above them?
Products
<div id="post-load" class="row products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN'
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
</div>
ACF Example (This works fine if placed above the products)
<?php
$features = get_field('features');
if( $features ): ?>
<div class="col-md-12"><h3 class="txtc"><?php echo esc_html( $features['title'] ); ?></h3></div>
<?php endif; ?>
I want to display category name on top and then the posts belonging to the particular category below it.
This should happen for all of my 20 categories and the results show be displayed on one page.
This is something I was trying but it doesn't work.
<?php $catquery = new WP_Query( 'cat=finance-training-seminars&posts_per_page=-1&post-type=dt_portfolio' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
You can use get_terms to list all categories and then create a query for each category and show posts related to it, something like this:
<?php
$categories = get_terms( array( 'taxonomy' => 'category' ) );
foreach( $categories as $cat ) :
$posts = new WP_Query( array(
'post_type' => 'dt_portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => array( $cat->term_id ),
'field' => 'term_id'
)
)
) ); ?>
<h3><?php echo $cat->name; ?></h3>
<ul>
<?php while( $posts->have_posts() ) : $posts->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php endforeach; ?>
Try this,
<?php
$rCateogry = get_the_terms( $post->ID, 'CUSTOM_TAXONOMY_NAME' );
$related = get_posts(array( 'post_type' => 'CUSTOM_POST_TYPE', 'tax_query' => array(array( 'taxonomy' => 'CUSTOM_TAXONOMY_NAME', 'field' => 'id', 'terms' => $rCateogry, )), 'showposts' => -1, 'order' => 'DESC', 'post__not_in' => array($post->ID) )); ?>
<section id="discover">
<h2>Related Posts</h2>
<div class="singlepost">
<?php if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php $cats = array();
foreach($rCateogry as $c){
$cat = get_category( $c );
echo $cat->name;
} ?>
<?php }
wp_reset_postdata(); ?>
</div>
</section>
Replace "CUSTOM_TAXONOMY_NAME" with the taxonomy name and "CUSTOM_POST_TYPE" with post type name
I have several posts with a custom field named "series". I want to group all posts by this custom field and below of this i want to list all posts which have not this custom field.
First i wanted to get grouped custom field value and then to be able to query again for all posts with this custom value key and value. But even trying to get the unique custom values does not work.
What i tried is this:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key = "series"';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array(
'meta_key' => 'series',
'ignore_sticky_posts' => 1
));
?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<ul>
<?php
while ( $states->have_posts() ) : $states->the_post();
$mykey_values = get_post_custom_values( 'series' );
foreach ( $mykey_values as $key => $value ) {
echo "<li>$key => $value ( 'series' )</li>";
}
endwhile;
?>
</ul>
Whats wrong with this code?
WP_Meta_Query
All posts with custom value:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'series',
'value' => 'my-val',
'compare' => '='
),
array(
'key' => 'series',
'value' => '',
'compare' => '!='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
And without values:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'series',
'value' => '',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
I'm trying to display a post type’s categories and output the posts within that category, but I have another taxonomy that I want to exclude. This code is showing all Taxonomies and Terms registered to the specific post type.
The second taxonomy that I am trying to exclude is named "manufacturer" and is registered to multiple post types. I have tried doing 'operator'=>'NOT EXISTS' as well as what you see below, with the if...continue but nothing seems to work !!
<?php
$post_type = 'equipment';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ):
if ( $term->slug == 'manufacturer' )
continue;
?>
<div class="row">
<div class="col-xs-12">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="col-xs-12">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h4><?php echo get_the_title(); ?></h4>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
)
$args = array(
'post_type' => array('equipment'),
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'manufacturer',
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
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();