0

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:\localhost\mywebsite\wp->includes\template.php on line 679

Notice: Trying to get property of non-object in C:\localhost\mywebsite\wp->includes\template.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.

3
  • 1
    Are you sure ht_kb is defined with title, excerpt and all that extra stuff?
    – kero
    Sep 23, 2017 at 9:41
  • @kero The plugin includes templates, which do include excerpt,category etc. and display correctly in the respective template parts. in my custom loop the_title() and the_author() display correctly, however the_content and the excerpt cause the error to show. excerpt from plugin: $args = array ('supports' => array('title', 'editor', 'author', 'comments', 'post-formats', 'custom-fields', 'revisions')); register_post_type('ht_kb', $args);
    – Samwyzz
    Sep 23, 2017 at 9:52
  • @kero The problem is definitely related to the theme, as the same loop works correctly without error on a different theme. I am new to WordPress and php, but I will try my best to search for a solution and post back later with results.
    – Samwyzz
    Sep 23, 2017 at 11:06

1 Answer 1

0

Hey this may be a little too late however I had this same issue on my custom made theme.

Firstly it turned out I didn't create any custom posts and secondly, with the loop, because there were no posts I didn't have the content-none file in my theme.

Once this was added the get_template_part( 'content-parts/content', 'none' ); code the error was gone.

Hope this helps someone else :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.