how to get related posts list in home page - wordpress

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.

Related

Group WordPress posts by category

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

get custom woocommerce product

i want to get 10 product from a category in woocommerce
for example, for get latest post of a posts category i use the following code
<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
<ul>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li><i class="circle"></i><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i want a code, like this for get woocommerce products
try this example :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
/* your loop */
Hope this will helps you.
Try this example,
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
OR
<?php
$args = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args );
?>
Hope this will helps you.
For more details please visit,
Woocommerce get products
why not use woocommerce's product_category shortcode?
<?php echo do_shortcode("[product_category category='17' limit='5']"); ?>
It will list the products same as in shop page. With these you are safe with product attributes like if when it's out of stock.

Wordpress get Custom post title

I'm new to WordPress programming
I created a WordPress postal warehouse called "wp_locations"
I need to show the title of this post on the page
I put the following code in the my theme index file
<?php $gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts( $gallery_args );
foreach($posts_display_gallery as $rows){
$post_title = $rows->post_title;
} ?>
But did not display the title
please guide me
Please use echo in your code
echo $post_title = $rows->post_title;
Please try below code and check it again....
<?php
global $post;
$gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts( $gallery_args );
foreach ( $posts_display_gallery as $post ) : setup_postdata( $post );
$post_title = $post->post_title; // Like this you will get post title.
echo $post_title; // For display
endforeach;
wp_reset_postdata();?>
Hope this will helpful for you.
<?php $gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts( $gallery_args );
foreach($posts_display_gallery as $rows){
$post_title = $rows->post_title;
echo $post_title; // for display the title
} ?>
You may try with this piece of code, should work
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ){
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$product_id = $loop->post->ID;
$product_title = $loop->post->post_title;
endwhile;
}
Try get_the_title() in the foreach loop

How to Hide post that have same category

is there someone who knows how to hide post that have same category?
I have Post 1, Post 2, Post 3 that have same category. Can i just show the latest post ( post 1) at my homepage, and hide other post ( post 2 and post 3 )
One way of doing that is to have three separate wp_queries.
//Query 1
$args1 = array(
'cat' => '1',
'nopaging' => true,
'posts_per_page' => '1',
); $query_1 = new WP_Query( $args1 );
if ( $query_1->have_posts() ) {
while ( $query_1->have_posts() ) {
$query_1->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
//Query 2
$args2 = array(
'cat' => '2',
'nopaging' => true,
'posts_per_page' => '1',
); $query_2 = new WP_Query( $args2 );
if ( $query_2->have_posts() ) {
while ( $query_2->have_posts() ) {
$query_2->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
//Query 3
$args3 = array(
'cat' => '3',
'nopaging' => true,
'posts_per_page' => '1',
); $query_3 = new WP_Query( $args3 );
if ( $query_3->have_posts() ) {
while ( $query_3->have_posts() ) {
$query_3->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
<?php
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
// the query
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Add your category ID to 'category'.
reefer these links for more information.
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
You can achieve this by alter the query of home page throught pre_get_posts hook.
Here is the code:
function txt_domain_get_distinct_post_id()
{
$post_ids = [];
//getting all non empty category
$categories = get_terms('category', array(
'orderby' => 'count',
'hide_empty' => 0,
));
foreach ($categories as $category)
{
$args = array(
'posts_per_page' => -1,
'category' => $category->term_id,
'exclude' => $post_ids,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
//getting post from a specific category
$postlist = get_posts($args);
foreach ($postlist as $post)
{
if (!in_array($post->ID, $post_ids))
{
//this will only select one post form each category
$post_ids[] = $post->ID;
break;
}
}
}
return $post_ids;
}
add_filter( 'pre_get_posts', 'txt_domain_filter_get_posts' );
function txt_domain_filter_get_posts($query)
{
if (is_home() && $query->is_main_query())
{
$post_ids = txt_domain_get_distinct_post_id();
$query->set('post__in', $post_ids);
}
return $query;
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Reference:
is_home()
get_terms
get_posts

Exclude duplicated posts from wordpress custom queries

I'm building a template for a homepage which shows 4 latest posts on top and some groups of posts divided by category around the page (I'm using get_posts to perform queries).
What I'd like to do is exclude from these category posts any post already present in the latest four news on top.
I guess I should get that four post IDs and use the 'post__not_in' parameter in the "category" queries, but I can't make it work.
Dou you have any hints?
This is the code:
// First query: I get last four posts
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
// In my dreams, this should be the array of post IDs
$ids->query_vars['page_id'];
// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query
$query = new WP_Query($args);
$args2 = array(
'numberposts' => 5,
'category' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
'post__not_in' => $ids
);
$query2 = new WP_Query($args2);
$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
... do the stuff ...
endforeach;
wp_reset_postdata();
UPDATE
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post();
$excludeID = $post->ID;
endwhile;
$args = array(
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'category' => 15,
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($excludeID)
);
$primaposizione = get_posts( $args );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
$category = get_the_category();
$slug = $category[0]->category_nicename ;
$esteso = $category[0]->cat_name;
if(has_post_thumbnail()) { ?>
<span class="hidden-xs"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></span>
<?php } ?>
<h3 class="ellipsis"><?php the_title(); ?></h3>
<?php the_excerpt();?>
<?php endforeach;
wp_reset_postdata();
?>
The way you're trying to select the ID's is not going to work. You have to call wp_query first. I think this should work:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post(); ?>
$excludeID[] = $post->ID; // forgot the brackets
// do stuff
endwhile;
(...)

Resources