How to Hide post that have same category - wordpress

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

Related

WordPress woo commerce get products by brand

In my WordPress project am trying to show products list based on brand name
This is my folder structure. Here am creating a API inside android folder.
In android/brands_products.php i want to show product list by brand name.
I tried this code:
<?php
require_once( '../wp-load.php' );
if ( woocommerce_product_loop() ) {
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*
* #hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
}
?>
But not working.
example: Pbs is brand name want to select Pbs brand name products
Please use below code :
<?php
require_once('../wp-load.php');
global $woocommerce;
global $product;
$brand_product_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'product_cat' => 'pbs',
'order' => 'desc',
'orderby' => 'date'
);
$brand_product_list = new WP_Query( $brand_product_args);
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$product_data = wc_get_product( $post->ID );
endwhile; wp_reset_query();
if(!empty($product_list))
{
$data['status']= true;
$data['product']= $product_data;
}
else
{
$data['status']= false;
$data['product']= array();
}
echo json_encode($data);
?>
require_once('../wp-load.php');
global $woocommerce;
global $product;
$brand_product_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'pwb-brand' => 'pbs',
'order' => 'desc',
'orderby' => 'date',
);
$brand_product_list = new WP_Query( $brand_product_args);
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$product_data = wc_get_product( $post->ID );
endwhile; wp_reset_query();
if(!empty($product_data))
{
$data['status']= true;
$data['product']= $product_data;
}
else
{
$data['status']= false;
$data['product']= array();
}
echo'<pre>'; print_r($data);exit;
echo json_encode($data);

Wordpress - Query comments by post meta

I am using the code below (simplified) to display a list of the last 10 comments:
<?php
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
//'meta_key' => 'field_name',
//'meta_value' => 'field_value',
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};
?>
Question:
Well, meta_key and meta_value seem to be associated to comment_meta... But in my case, I have to display comments based on post_meta key and value.
Any sugestions?
You can try this code.
You need to add a query for posts to get array of post ides with meta key.
Then use that array into comments query argument.
//QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
$post_args = array(
'post_type' => 'post',
'meta_key' => 'meta key',//Meta key of post
'meta_value' => 'meta value',//String or Numeric value
'meta_compare' => '=',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
Try this , then let me know the result.
My first question here at Stackoverflow and that worked perfectly.
Thank you very much, Souvik!
Below the final result (simplified):
$post_args = array(
'post_type' => 'tarefa',
'posts_per_page' => -1,
'meta_key' => 'field_name',
'meta_value' => 'field_value',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'number' => '30',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};

How to call category in dashboard if mine posted is attached to them only?

I am making user dashboard and I create a dashboard widget for category. Now I want to show category if current user's post is attached to them.
Currently I am able to see all categories.
Here is my function
function user_categories() {
wp_add_dashboard_widget(
'wp_widget_category', // Widget slug.
'Categories', // Title.
'my_dashboard_category' // Display function.
);
function my_dashboard_category() {
if ( is_user_logged_in() ):
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
?>
<?php
$args = array(
'type' => 'recipes',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'author' => $current_user->ID,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'recipe_categories',
'pad_counts' => false );
$categories = get_categories($args);
foreach ($categories as $category) {
$url = get_term_link($category);?>
<div class="submitted_recipe">
<a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?>
<h2><?php echo $category->name; ?></h2>
</a>
</div>
<?php
}
?>
<?php
}
endif;
}
}
add_action( 'wp_dashboard_setup', 'user_categories' );
You need to query all user posts in each categorie, if it's not empty then include this category. try this revised code
function user_categories() {
wp_add_dashboard_widget(
'wp_widget_category', // Widget slug.
'Categories', // Title.
'my_dashboard_category' // Display function.
);
function my_dashboard_category() {
if ( is_user_logged_in() ):
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
?>
<?php
$args = array(
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'recipe_categories',
'pad_counts' => false );
$categories = get_categories($args);
foreach ($categories as $category) {
$posts_query = new WP_Query( array(
'post_type' => 'recipes',
'author' => $current_user->ID,
'tax_query' => array(
array('taxonomy' => 'recipe_categories',
'field' => 'slug',
'terms' => $category->slug)
),
'fields' => 'ids'
) );
$ids = $posts_query->posts;
if (!empty ($ids)) {
$url = get_term_link($category);?>
<div class="submitted_recipe">
<a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?>
<h2><?php echo $category->name; ?></h2>
</a>
</div>
<?php
}
}
?>
<?php
}
endif;
}
}
add_action( 'wp_dashboard_setup', 'user_categories' );

Get Posts from custom taxanomy

I want to get posts with custom taxonomy, you can check the code below ppctrainings is the custom taxonomy category and 94 is the id of category.
<?php
$count = 0;
// this is the custom taxonamy ppctrainings the id is 94
$posts = get_posts('ppctrainings=94&numberposts=10');
foreach($posts as $post) {
if($count == 3){
echo '</tr><tr >';
$count = 0;
}
$count++;
?>
<?php
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'sliders_category', // taxonomy category
'field' => 'id',
'terms' => '2' // category id
)
),
'post_type'=>'sliders', //name of post type
'order'=>'ASC',
'posts_per_page'=>10
);
query_posts($args);
while ( have_posts() ) : the_post();
?>
// your code for display title and content
<?php
endwhile;
wp_reset_query();
?>
Use the below code for getting the custom post type data.
$args = array(
'posts_per_page' => '-1',
'post_type' => 'blog',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'post_date');
$the_query = new WP_Query($args);
while ($the_query->have_posts()) {
$the_query->the_post();
// Write your code here
}
There are many ways you can query the WordPress database
pre_get_posts action
query_posts()
the WP_Query

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.

Resources