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
Related
I have made this code and the variable $link is not returning the product url, instead is returning home url. Please some Help! What I want is to get the product link selecting it by category. The display is like a menu by category but with the link going directly to one product I selected by category.
<ul>
<?php
$categories= get_terms( 'product_cat', array( 'parent' => 15, 'hide_empty' => false ) );
foreach($categories as $category): ?>
<li>
<div class="grid-subheader">
<?php
$metaArray = get_option('taxonomy_' . $category->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$type = get_term_by( 'id', $category->parent, 'product_cat' );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category->term_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
foreach ($products as $prod){
$link = get_the_permalink($prod->ID);
}
?>
<h3><?php echo $productCatMetaTitle; ?></h3>
<p><?php echo $category->description; ?></p>
<a href="<?php echo $link; ?>">
<div class="button-menu">
<?php //echo $type->name; ?> <?php echo $category->name; ?>
</div>
</a>
</div>
</li>
<?php endforeach; ?>
</ul>
Try the following revisited code:
<ul>
<?php
$taxonomy = 'product_cat';
$category_terms = get_terms( $taxonomy, array( 'parent' => 15, 'hide_empty' => false ) );
foreach( $category_terms as $term ) { ?>
<li>
<div class="grid-subheader">
<?php
$meta = get_option('taxonomy_' . $term->term_id);
$title = isset($meta['wh_meta_title']) ? $meta['wh_meta_title'] : '';
$type = get_term_by( 'id', $term->parent, $taxonomy );
$query = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term->term_id ),
) ),
) );
$product_id = reset($query);
?>
<h3><?php echo $title; ?></h3>
<?php if ( ! empty($term->description) ) ?>
<p><?php echo $term->description; ?></p>
<a href="<?php echo get_permalink($product_id); ?>">
<div class="button-menu"><?php echo $term->name; ?></div>
</a>
</div>
</li>
<?php
}
?>
</ul>
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 list my categories in a page using the following code:
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = count($terms);
$categories = array();
if ($count > 0) :
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){
$post_from_category->the_post();
}else{}
$term->slug;
$term->name; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
<a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?php echo $term->name; ?></span>
</header><!-- .entry-header -->
</a>
</article><!-- #post-## -->
<?php }
endif; ?>
My question is: How can I list the categories which contains minimum 5 posts only?
Thank you very much!
Inside your foreach loop you can just check to make sure $term->count is greater than or equal to 5. There's also a few other things I noticed:
You define a $categories array but don't appear to use it again.
Try not to mix if: endif; and if{} statements - stick to a syntax for ease-of-maintenance.
You can just check to see if $terms gets set to a truthy value instead of checking its count.
Are you sure your Tax Query is set properly? It's checking the taxonomy "categorys" which is a typo of "categories"
You don't need an empty else{} loop after your ->have_posts() check
You don't need to instantiate the $term object variables before the article.
You should consider using home_url() instead of bloginfo('url')
All that said, this should get you started:
<?php
$term_args = array(
'taxonomy' => 'category',
'hide_empty' => true,
);
if( $terms = get_terms( $term_args ) ){
foreach( $terms as $term ){
if( $term->count >= 5 ){
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){ $post_from_category->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'thumb-block' ); ?>>
<a href="<?= home_url( "?categories={$term->slug}" ); ?>" title="<?= $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?= $term->name; ?></span>
</header>
</a>
</article>
<?php }
}
}
}
?>
I would like to get a pager in my page, can i create a pager with a for each or while without parameter like "posts_per_page" ?
I get term children of my taxonomy and i would like to display 3 by 3
My code :
$childGalerie = get_term_children(15,'wpmf-category');
foreach ( $childGalerie as $oneGalerie ) :
$maGalerie = get_term($oneGalerie, 'wpmf-category');
?>
<h2 class="info-galerie"><span></span><?php echo $maGalerie->name; > </h2>
<div class="galerie-photo">
<?php
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'attachment',
// 'post_status' => 'inherit',
'tax_query' => array(
array(
'taxonomy' => 'wpmf-category',
'field' => 'term_id',
'terms' => $oneGalerie
),
),
);
$media = get_posts( $args );
foreach ( $media as $post ) : setup_postdata( $post );
?>
<div class="wrapper-galerie">
<?php echo wp_get_attachment_image( $media->ID, 'galerie->photo' ); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
</div>
<?php
endforeach;
Can you help me please , ty :D
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; ?>