Get Wordpress Category ID from a Post ID - wordpress

I am using a custom post type and custom categories. I need to fetch the category ID of a custom post type in a page. I tried using various methods but I am unable to get the category ID.
<?php
$args = array(
'posts_per_page'=> -1,
'post_type' => 'professional',
'orderby'=> 'id',
'order'=> 'asc',
);
query_posts($args);
$my_posts = new WP_Query($args);
while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts
function getCurrentCatID(){
global $wp_query;
global $post;
$cats = get_queried_object()->term_id;
$cat_ID = $cats[0]->cat_ID;
return $cat_ID;
}
?>
<li>
<div class="caption">
<h3><?php the_title(); ?></h3>
<span class="industry"><?php getCurrentCatID($cat_ID); ?></span>
</div>
</li>
I am not able to get the cat ID in custom post types.

Try This.
$cat_l=get_the_term_list( $post->ID, 'custom_categories', '', ' , ', '' );
echo $cat_l;

Related

Get post publish date inside ACF foreach

In the post loop I have a foreach loop which displays selected team members in the admin(from the "team" custom post type) from an ACF post object.
What I want to achieve is to display the post-publish date inside the foreach.
The problem is that the code echo get_the_date('M d, Y'); shows the date of the "team" custom post type and not the blog post date because is in the foreach.
I tried to pass the post id but it's shows the posts id of the "team" post types and not the actual "post".
How can I show the 'post_type' => 'post' publish post date inside in the foreach?
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DSC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$postAuthors = get_field('select_post_author'); // ACF post object - custom post type - team
if( $postAuthors ): ?>
<?php foreach( array_slice($postAuthors, 0, 1) as $post): ?>
<div class="post-date">
<span>
<?php
echo get_the_date('M d, Y'); // here I want to get the post publish date but it show the publish date of the team post type items
?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Thanks
So the issue is due to $post being a global variable. I assume ACF stores an array of posts for get_field('select_post_author') then you need to change your foreach statement local variable name, e.g:
<?php foreach( array_slice($postAuthors, 0, 1) as $postAuthor): ?>
<div class="post-date">
<span>
<?php echo get_the_date('M d, Y', $postAuthor->ID); ?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
I solved the issue by passing the post id in the date.
<?php
$pid = get_the_ID(); //get the post id
$postAuthors = get_field('select_post_author');
 if( $postAuthors ): ?>
Pass the post id in the get_the_date.
echo get_the_date('M d, Y',$pid);

Get multiple custom taxonomy term names in a query loop - Wordpress

I'm facing an issue with my website.
I would like to display all my custom post type named "Projets", and for each item, I want to get several term names to put in my data element.
Displaying all my post is not an issue, it's working well. I manage to display one term name using "get_terms()", but I don't know how to display several terms and put them in the right place.
I have 3 different custom taxonomy : city, typo and statut.
There is my code :
<?php
$args = array(
'post_type' => 'projets',
'posts_per_page'=>'99',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="content_item active" data-city="CITY_NAME_HERE" data-typo="TYPO_NAME_HERE" data-statut="STATUT_NAME_HERE">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('custom-size', ['class' => 'content_item_img', 'title' => 'Image du projet']); ?>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile;
wp_reset_postdata();
?>
Thanks for the help!
Try this:
Inside the loop, you get the taxonomy like this (eq. "city"):
$cities = get_terms( array(
'taxonomy' => 'city',
'hide_empty' => false,
) );
You get an array of all the terms of the Taxo.
Where you need them, you have to loop them, here to give you an idea.
if(!empty($cities)){
foreach($cities as $city){
echo $city->name.', ';
}
}else{
echo 'No Cities.';
}
Comma Separated custom categories inside the loop.
<?php echo get_the_term_list( $post->ID, 'cutom_category', '', ', ', '' ) ?>

Plugin Post 2 Posts: How to show the latest connected child post from parent post in loop?

I want to list latest child connected post ordered by parent post. Is it possible? Could anyone help me with that? I explain with an example: let's say I have Dramas custom post type and an Episode custom post type, that are connected to Posts 2 Posts. In the Drama archive, I want to list all the dramas by their latest episode and want to show only one episode in the loop.
I think it is maybe possible with each_connected(), but I don't know how to order the $wp_query array to get this done.
Thanks.
I am using with such kind of code now
<?php
global $post;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$anime_series = array(
'post_type' => 'drama',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
$the_query = new WP_Query( $anime_series);
$extra = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
p2p_type( 'drama_to_episode' )->each_connected( $the_query, $extra, 'episode' );
?>
</div>
<div class="last_episodes loaddub">
<ul class="items">
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<div class="img"> <img alt="<?php echo the_title(); ?>" itemprop="photo" src="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="type"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/SUB.png"></div>
</div>
<?php
foreach ( $post->episode as $post ) : setup_postdata( $post ); ?>
<p class="name"> <?php echo get_the_title($items['parent']); ?> </p>
<p class="episode"> <?php echo the_field('short_name'); ?> </p>
<?php
endforeach;
?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
</ul>
</div>
Try this easy example
?php
$args = array(
'post_parent' => 38,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any'
);
$children = get_children( $args );
foreach($children as $post):
echo $post->post_title;
endforeach;
?>
Hope that work it in your requirement!!!
Luckily, I solved my problem myself. My primary concern was to get the latest connected child post with its parent. Before in default loop, all the episodes (old/new) were being retrieved. I made a custom select query for Posts to Posts plugin. This query returned the IDs of the latest connected post. The working code is given below. Kindly, correct me if I am using the wrong programming standards.
global $post, $items, $wpdb;
$table_name = $wpdb->prefix . "p2p";
$query = 'SELECT p2p_from as anime, max(p2p_to) as episode FROM '.$table_name;
$results = $wpdb->get_results( $query.' GROUP BY p2p_from ORDER BY episode DESC' );
Then, in foreach loop, I retreived the post title, permalink etc by the ID.

Returning the name of a category in Wordpress

I have a loop for a custom post type an all is working well, except I cannot return the actual category name that the post has assigned to it.
I have the following code:
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($posts) {
foreach($posts as $post) {
setup_postdata( $post );
?>
<div>
<div class="mix <?php get_cat_name( ); ?>" ><img src="<?php the_field('portfolio_image'); ?>" alt=""></div> <!-- ACF -->
</div>
<?php
}
}
wp_reset_postdata();
?>
This is just one of the WP Codex functions I have tried including tring all with echo. I imagine my problem is something else?
Many thanks in Advance.
According to the codex an id is required for this method (https://codex.wordpress.org/Function_Reference/get_cat_name). Leaving out the id will not work.
One solution would be to use get_the_category($postId) to retrieve the category for your post (https://codex.wordpress.org/Function_Reference/get_the_category). You can leave out the postId here
post_id
(integer) (optional) The post id.
Default: $post->ID (The current post ID)
Note that the method returns an object - not the category name. You can try the following code to see if it's working:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
try this code
<?php $terms = get_the_terms($post->ID, 'TAXONOMY' );
if ($terms && ! is_wp_error($terms)) :
$term_slugs_arr = array();
foreach ($terms as $term) {
$term_slugs_arr[] = $term->name;
}
$terms_slug_str = join( " ", $term_slugs_arr);
endif;
echo $terms_slug_str;?>
Replace your code
from
get_cat_name( );
to
$categories = get_the_terms($post->ID,'custom-texonomy-name');
$cat = key($categories);
echo $categories[$cat]->name;

Wordpress : How canget category name for post from post type

I've got post_type = foods in my wordpress page, and I need to get the category of each post in this post type.
how can i do that.
this code is to det info of post :-
$ps = new wp_Query( array( 'post_type' => 'foods' , 'posts_per_page' => '6' ) );
if($ps->have_posts())
{
while($ps->have_posts())
{
$ps->the_post();
?>
<div class="reviews-left1"><? the_title(); ?></div>
<div class="reviews-left1">Category : </div>
<? }} ?>
As you have a custom post_type I assume you have also a custom taxonomy related to the Food that you call "Category"
Here is a function which will return all terms of a taxonomy for a post:
<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ); ?>
Your code should be:
<?php $ps = new wp_Query( array( 'post_type' => 'foods' , 'posts_per_page' => '6' ) );
if($ps->have_posts())
{
while($ps->have_posts())
{
$ps->the_post();
$food_cateogries = wp_get_post_terms( get_the_ID(), 'YOUR-TAXONOMY-ID');
?>
<div class="reviews-left1"><? the_title(); ?></div>
<div class="reviews-left1">Category : <?php echo implode(',', $food_categories); ?></div>
<? }} ?>
See more here http://codex.wordpress.org/Function_Reference/wp_get_post_terms

Resources