Custom post type loop error: Trying to get property of non-object

Question

A plugin I am using creates a custom post type for articles ie. ‘ht_kb’.

In my themes front page I have a custom loop displaying recent posts of the standard ‘post’ type. On the same front page I have another custom loop displaying recent posts of the plugins custom ‘ht_kb’ post type. The standard post type loop works as expected, but with the custom post type loop I am getting the error as posted below.

I am having difficulty understanding the reason for this error and would really appreciate if someone could explain what i am doing wrong and what is the correct way of looping through a custom post type.

The loop below works just fine with the standard ‘post’ type.

//The Query
$post_args = array(
'post_type' => array('post'),
'posts_per_page' => 4
);

$post_query = new WP_Query($post_args);

// The Loop
if ($post_query->have_posts()) {

while ($post_query->have_posts()) {

    $post_query->the_post();
    the_title();
    the_category();
    the_date();
    the_author();
    the_excerpt();
}

//reset loop

wp_reset_postdata();

}

?>

The custom post type loop below returns the error:

Notice: Trying to get property of non-object in C:localhostmywebsitewp->includestemplate.php on line 679

Notice: Trying to get property of non-object in C:localhostmywebsitewp->includestemplate.php on line 679

Loop for the custom post type

<?php

// The Query
$article_args = array(
'post_type' => array('ht_kb'),
'posts_per_page' => 4
);

$article_query = new WP_Query($article_args);

// The Loop
if ($article_query->have_posts()) {

while ($article_query->have_posts()) {

    $article_query->the_post();
    the_title();
    the_category();
    the_date();
    the_author();
    the_excerpt();
}

//reset loop

wp_reset_postdata();
}
?>

note: If I comment out the_excerpt(); function, The loop does not display the error and (sort of) works as intended although other issues such as, the_category() function does not return any value.

Thanking you for your time and help.

0
, Sameer Ali 2 years 2019-12-28T13:27:46-05:00 0 Answers 81 views 0

Leave an answer

Browse
Browse