Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

agsharad07 on "Trying to get property of non-object in WP_Query"

$
0
0

I am creating a sample portfolio showcase template. In that i have created categories like products,services,etc. Now i have created a page to display each kind of post of each category. Like a page for product category,one for service.....
I have kept the slug name of category-page pair same i.e. Product category and product page have same slug. SO that i may fetch the slug of current page and display all posts of it from that named category.

But whether i use WP_Query or $wpdb both fails giving following error:

Notice: Undefined offset: 1 in E:\xampp\htdocs\wp-test\wp-includes\query.php on line 3680

Notice: Trying to get property of non-object in E:\xampp\htdocs\wp-test\wp-includes\query.php on line 4553

Notice: Trying to get property of non-object in E:\xampp\htdocs\wp-test\wp-includes\query.php on line 4555

Notice: Trying to get property of non-object in E:\xampp\htdocs\wp-test\wp-includes\query.php on line 4557

Also that the content i had written at the time of page creation is visible, but none of the posts.... why ??

My Code:

Note: that code i used for wpdb is commented.

<?php
//Template Name: my theme
get_header();
?>
<div id="maincon" class="container">
 <div id="content-con" class="content">

	<?php
	if(have_posts()): while(have_posts() ): the_post() ;
   ?><div class="row">
	<h1 id="mainhead" class="text-center"><?php the_title();
	endwhile;
	endif;?></h1>	  </div>

	<div class="row"><div class="col-md-offset-1 col-md-10">
 	<?php
	 global $post;
	$slug = get_post( $post )->post_name;
     // global $wpdb;
	 // $qu="select <code>post_title</code>,<code>post_content</code> from <code>wp_posts</code> inner join <code>wp_term_relationships</code> on <code>wp_term_relationships</code>.object_id=ID inner join <code>wp_terms</code>on <code>wp_terms</code>.term_id=<code>wp_term_relationships</code>.term_taxonomy_id where slug='".$slug."'";
	 // $res=$wpdb->query($qu);

	$args=array('post_type'=>'post',
				// 'name'=>$slug
				'tax-query'=>array('taxonomy'=>'category',
									'field'=>'slug',
									'terms'=>$slug
									)
				);
	//$args=array('name'=>$slug,'post_type'=>'post');
			$query=new WP_Query($args);
			if($query->have_posts()):while($query->have_posts()): the_post();

	// if(!empty($res)){
	?>
	<h2 class="text-center" id="article-head"><?php the_title();//  echo $res[0]->post_title;?></h2>
	<p><?php the_content();//the_excerpt();echo $res[0]->post_content; ?></p>

	<?php
	// }
		// else{
			// echo "sory";}
		  endwhile;wp_reset_postdata();endif; ?>
		</div>
	</div>
 </div>

</div>

<?php get_footer(); ?>

Viewing all articles
Browse latest Browse all 8245

Trending Articles