wp_get_recent_posts() exclude specific post by id - wordpress

I am trying to get the two most recent post but however i want to exclude a specific post by its id[365]. Can anyone help me with it? here's my code.
$args = array( 'numberposts' => '2' , 'post__not_in' => array( '365' ) );
$recent_posts = wp_get_recent_posts( $args );
<?php foreach($recent_posts as $post):?>
<p><?php echo $post['post_title'];?></p>
<?php endforeach;?>

Just need to replace your code by below mentioned code, you will get your desired result :
<?php $my_args = array('post_type' => 'post' , 'numberposts' => '2' , 'exclude' => '365' );
$my_recent_posts = wp_get_recent_posts( $my_args );?>
<?php foreach($my_recent_posts as $my_post):?>
<p><?php echo $my_post['post_title'];?></p>
<?php endforeach;?>

You should read the docs for that function again. You have an exclude parameter available:
$args = array( 'numberposts' => '2' , 'exclude' => 365 );

wp_get_recent_posts Does not support post__not_in argument you will need to use exclude
Reference link for wp_get_recent_posts
<?php
$args = array( 'numberposts' => '2' , 'exclude' => '365' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post){
?>
<a href="<?php echo $post['guid']; ?>">
<p><?php echo $post['post_title']; ?></p>
</a>
<?php } ?>

Related

How do I echo the post term ID for a custom taxonomy in a CPT loop?

I'm looking to echo the term ID for a custom taxonomy in a cpt loop.
Here is where I am at but can't seem to find a solution.
<?php $loop = new WP_Query(array(
'post_type' => 'companies',
'posts_per_page' => -1,
'orderby'=> 'ASC'
)); ?>
<?php
$taxonomy_prefix = 'company_category';
$term_id = 'TERM ID HERE';
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
?>
<?php endwhile; wp_reset_query(); ?>
</div>
Suggestions appreciated!
Thanks,
global $post;
wp_get_object_terms( $post->ID, 'your_taxonomy_name', array( 'fields' => 'ids' ) );
you can use this function.

How to list all category from custom post type?

I have a post type called 'dining' and has a taxonomy called 'dining-category'.
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
In WordPress 4.6 get_terms is deprecated. So there is an alternate of this (get_categories) Read this
And here is Example code:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><?php echo $category->name; ?></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><?php echo $wcatTerm->name; ?></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.

How can I get the latest post from a category in WordPress?

I am designing the category page. I have a loop that displays the child categories for the current category. For each child category, I would like to display a link to the latest article. Currently, the link is the same for all child categories, even when the displayed article is not in that category. What am I doing wrong?
<?php
$cat_id = get_query_var('cat');
$categories = get_categories(array( 'parent' => $cat_id));
if(count($categories) > 0):
foreach($categories as $cat):
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => $cat->cat_ID,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$the_query = new WP_Query( $args );
$the_query->the_post();
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$recent['title'] = get_the_title();
$recent['id'] = get_the_ID();
wp_reset_postdata();
endwhile;
endif;
wp_reset_postdata(); ?>
<div class="media category-list">
<div class="media-body">
<div class="details">
<h3><?php echo $cat->name; ?></h3>
<p><?php echo $cat->description; ?></p>
</div>
<dl>
<dt>Article Total:</dt><dd><?php echo $cat->count; ?></dd>
<dt>Last Article:</dt><dd><?php echo substr($recent["title"], 0, 48).'...'; ?></dd>
</dl>
</div>
</div>
<?php endforeach;
endif; ?>
Looks like you are using get_posts arguments in WP_Query.
category and numberposts are not valid arguments for WP_Query
Both belongs to get_posts and internally converted to cat and posts_per_page
So when you pass these arguments in WP_Query it does not work. But when you pass WP_Query arguments in get_posts it works ;)
So the updated arguments structure is
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'cat' => $cat->cat_ID,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
Also please update your query with this
$the_query = new WP_Query( $args );
//$the_query->the_post();
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$recent['title'] = get_the_title();
$recent['id'] = get_the_ID();
// wp_reset_postdata();
endwhile;
endif;
wp_reset_postdata();
the_post should be called only once and after when you are sure that query has posts.
wp_reset_postdata hold the data for whole query. So need to do this at end of while loop not inside of the while loop.

Error trying to get the taxonomy term name

Hello—so what I'm attempting is to get 2 posts from 2 taxonomy terms, I've got that part done, where I'm borking up is trying to display the term name above the posts. I've done this before using a foreach loop but no matter what I change, I keep getting the error that I've supplied invalid arguments for the loop. After a lot of googling, I'm a bit lost and wondering if you folks have some guidance for me?
$current_post_id = get_posts( array(
'post_type' => 'issue',
'posts_per_page' => 1,
'fields' => 'ids',
) );
$terms = get_the_terms( $current_post_id, 'department' );
foreach ( $terms as $term ) {
$args = array(
'connected_type' => 'posts_to_issues',
'connected_direction' => 'to',
'connected_items' => $current_post_id,
'post_type' => 'post',
'posts_per_page' => 2,
'tax_query' => array(
array(
'taxonomy' => 'department',
'field' => 'slug',
'terms' => array( 'washington-watch', 'equipment-spotlight'),
)
),
);
$sidebar_query = new WP_Query( $args );
while ( $sidebar_query->have_posts() ) : $sidebar_query->the_post(); ?>
<?php echo '<h2>' . $term->name . '</h2>' ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'sidebar-thumb-med' ); ?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<?php the_excerpt();
endwhile;
}
// Restore global post data
wp_reset_query();
$current_post_id is an array with one item (because passing in a return fields argument returns an array). You need to refer to that one item in the following cases:
$terms = get_the_terms( $current_post_id[0], 'department' );
...and...
'connected_items' => $current_post_id[0],

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