The value is not fetching and displaying in this plugin. When I add the code elsewhere in the theme it's working properly.
<?php
function related_posts( $atts ) {
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 5,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<li style="list-style: none;"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
$gread = get_post_meta($post->ID, 'read_time', true);
$gdate = get_the_date();
echo '<span style="font-size:12px; margin-left: 22px;"> Read Time: '.$gread.' Minutes </span>';
echo '<span style="font-size:12px; margin-left: 22px;"> Posted: '.$gdate.' </span>';
endwhile;
// reset $post after custom loop end
}
add_shortcode( 'relatedpost', 'related_posts' );
The value is not fetching and displaying in this plugin. When I add the code elsewhere in the theme it's working properly.
<?php
function related_posts( $atts ) {
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 5,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<li style="list-style: none;"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
$gread = get_post_meta($post->ID, 'read_time', true);
$gdate = get_the_date();
echo '<span style="font-size:12px; margin-left: 22px;"> Read Time: '.$gread.' Minutes </span>';
echo '<span style="font-size:12px; margin-left: 22px;"> Posted: '.$gdate.' </span>';
endwhile;
// reset $post after custom loop end
}
add_shortcode( 'relatedpost', 'related_posts' );
You don't have the $post
object, but trying to use it in the code ($post->ID
). Use get_the_ID()
function instead:
<?php
while ( $cat_query->have_posts() ) : $cat_query->the_post();
// $gread = get_post_meta( $post->ID, 'read_time', true ); // Wrong
$gread = get_post_meta( get_the_ID(), 'read_time', true ); // Right
endwhile;
}
From now forth always enable debugging when you develop. In such a way you'll avoid wasting your time and asking unpractical questions.
get_the_ID()
returns inside the function? – Max Yudin Commented Dec 14, 2018 at 12:10