Group WordPress posts by category - wordpress

after several unsuccessful searches, I ask my question here.
Indeed, I'm trying to display a list of posts grouped by categories:
CAT A
post1
post2
post3
CAT B
post4
post5
post6
post7
...
Here is the code I tried.
I can display the categories, but not the posts
<?php
$terms = get_terms( 'secteur', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>

You need to use tax_query as an WP query attribute, instead of secteur.
Try replacing that with:
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'secteur',
'field' => 'slug',
'terms' => $term -> slug,
),
),
);

thank you very much for your response.
Unfortunately, this does not change the display. The titles are displayed but not the articles.
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
If it helps, I can display all the items with the following code :
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'client',
'post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><img src="'.get_field( 'logo' ).'"></div>';
}
echo '</div>';
}
?>

Thank you very much for your answers.
But nothing has worked for me, and I can’t come up with any solutions.
I think the problem lies in the configuration of my taxonomy.
This is my custom post type configuration (client):
https://imgur.com/uDom3PH
my taxonomy configuration (secteur) :
https://imgur.com/WRwsSbR
my custom field (secteur_dactivite) :
https://imgur.com/NKQ4GPn
Thanks again for your help

Related

Wordpress: I want latest one product from all categories display on the page in wordpress?

I want latest one product from all categories display on the page in wordpress , when we add more categories and products in it then it should have to be add(display) on the page with its single latest product.How we can do this? please help me. Thank you
First, you need to get all the product categories with at least a single post using the hide_empty.
Then loop through each category and run a query for each to get the single product.
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4>' . $product_category->name . '</h4>';
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
)
),
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
wp_reset_postdata();
echo "</ul>";
}
}

wordpress custom post type loop by tag id

I'm trying to make a loop of a custom type of posts by tag ID.
This is the original code:
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
Since I need to loop only the posts in a certain category (of custom posts type) having id 31 I added "'tag_id' => 31" after "'posts_per_page' => -1"
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1,
'tag_id' => 31
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
[EDIT]
I have another loop below in the page and I tried to insert 'cat'=>31 tax_query array
<?php $number_posts_to_display = get_field('number_of_posts_to_display');
$display_order = get_field('post_display_order');
$portfolio = array(
'post_type' => 'kana_portfolio',
'posts_per_page' => $number_posts_to_display,
'order' => $display_order,
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'tax_query' => array(
array(
'taxonomy' => 'kana_genre',
'field' => 'slug',
'terms' => $portfolio_term_array,
),
),
);
$portfolio_loop = new WP_Query($portfolio); ?>
But nothing is showed, how can I do?
if your category (we are talking real categories here, of the taxonomy type category...) is 31, you should use 'cat'=>31, tags are tags, they are not categories.. if this helped, let me know. if you were trying to filter on tags and it was not working, let me know as well and ill take a look in depth.
Have fun!
Instead of:
'tag_id' => 31
Try this:
'tag__in' => 31

how to get related posts list in home page

how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.

Wordpress Custom Post Meta Query

I'm trying query a custom post type for each comment with the dynamic field "comment_ID." I'm using the code below. This currently shows the comment ID, which I don't want, but does not show 'paid' as I would like.
<?php
$commID = comment_ID();
$args = array( 'post_type' => 'paidbriefs', 'meta_key' => 'Comment_ID', 'meta_value' => 'echo $commID', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo 'paid';
endwhile; ?>
</p>
I'm obviously doing something wrong with echoing the $commID variable as this does not show anything. If I change this to just $commID it returns 'paid' for every comment that has a Comment_ID meta, regardless of whether it matches the actual comment ID. Does anyone know how to fix this?
I think what you will need to do is a Loop, within a loop,
first loop to generate the standard loop, this will have your post info, comments etc.
within that loop you need to declare your comment_ID;
then from there, you setup another internal loop,
using the comment_ID for your custom field,
<?php
$args = array( 'post_type' => 'paidbriefs', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$commID = comment_ID();
$innerargs = array( 'post_type' => 'paidbriefs',
'meta_key' => 'Comment_ID',
'meta_value' => $commID,
'posts_per_page' => 10 );
$innerloop = new WP_Query( $innerargs );
while ( $innerloop ->have_posts() ) : $innerloop ->the_post();
echo 'paid Comment';
endwhile;
endwhile;
?>
untested though.
hopefully this will help,
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;?>
you can try this one

Wordpress - Loop with images from post to

I made an Wordpress theme, with pages and posts.
The loop of posts show me a short brief of post and a Continue reading link.
I like this, but how can I make the theme show in the post brief of the loop image(s) attached to post at beginning, if any.
Thank you!
You can get your attached images by using:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID
);
$images = get_posts($args);
and display it like this:
echo wp_get_attachment_image($images[0]->ID, $size='attached-image');
This for getting all attachement images with your post.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
$img = wp_get_attachment_image_src($post->ID, 'medium');
$fullsize = wp_get_attachment_image_src($post->ID, 'full');
}
}
You should add in your loop:
<?php
if(has_post_thumbnail()) {
$theimage = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'thumbnail' );
}
?>
<img class="img_class" src="<?php echo $theimage[0]; ?>" />
Where "thumbnail" correspond to the size you want to show.
Remember that there is also a WordPress specific site in StackExchange

Resources