Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am running the really-simple-facebook-twitter-share-buttons. I have placed the below code on my site as the plug in recommends, which is:

<div class="share"><?php echo really_simple_share_publish();?></div>

This plugin is working on all of my site, except I have a custom taxonomy page that is causing the following error with the plugin:

Notice: Trying to get property of non-object in /home3/answr/public_html/wp-includes/post-template.php on line 29

I have reviewed line 29 and here is what I think is happening.

My taxonomy page has two main custom queries built into it and two wordpress loops. These queries modify the main wordpress query for the page.

It seems if the first query does not return results then it causes the error above if the plugin is active.

However, if the first query returns results then there is no error and the plugin works fine even if the second query returns results.

And if the plugin is not active I get no error whether the first query returns results or not.

Here is my code for reference. I have been trying to solve this for some time and I am out of ideas. Does anyone have ideas what could be causing this issues or what I should try to solve it. Thank you!

<?php

    query_single('dealers', 'publish', '1', $taxtype, $value);
?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<?php 

$address=get_post_meta($post->ID, 'wpcf-street_address', TRUE); 
$city=get_post_meta($post->ID, 'wpcf-city', TRUE); 
$state=get_post_meta($post->ID, 'wpcf-state_abbreviation', TRUE); 
$zip=get_post_meta($post->ID, 'wpcf-zip_code', TRUE); 
$phone=get_post_meta($post->ID, 'wpcf-phone_number', TRUE); 
$paid=get_post_meta($post->ID, 'wpcf-paid', TRUE);
$post_id=get_the_ID();
get_each_dealer_brand($post_id);?>

<?php 
echo "<ul class=\"ullisting\">";
if($paid==1)
{
    echo "<li><p class=\"plisting\"><strong><a href=\"";the_permalink(); echo "\">";the_title();echo "</a></strong></p></li>";
    echo "<li><p class=\"plisting\">$address | $city, $state $zip</p></li>";
    echo "<li><p class=\"plisting\">P: $phone</p></li>";
        echo "<li><p class=\"listing\"><span><small>$brands_list</small></span></p></li>";
}
echo "</ul>";
?>

<?php endwhile; ?>

<?php

wp_reset_query(); 
wp_reset_postdata(); 
unset($brands_list);
?>

<?php else : ?>

<?php


$paidnull="true";
?>

<?php endif; ?>

<?php

    query_single('dealers', 'draft', '0', $taxtype,  $value);
?>


<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>



<?php $address=get_post_meta($post->ID, 'wpcf-street_address', TRUE);
$city=get_post_meta($post->ID, 'wpcf-city', TRUE);
$state=get_post_meta($post->ID, 'wpcf-state_abbreviation', TRUE);
$zip=get_post_meta($post->ID, 'wpcf-zip_code', TRUE);
$phone=get_post_meta($post->ID, 'wpcf-phone_number', TRUE);
$paid=get_post_meta($post->ID, 'wpcf-paid', TRUE);
$post_id=get_the_ID();
get_each_dealer_brand($post_id);
?>


<?php 
echo "<ul class=\"ullisting\">";
if($paid==0){
    echo "<li><p class=\"plisting\"><strong>";the_title();echo "</strong></p></li>";
    echo "<li><p class=\"plisting\">$address | $city, $state $zip</p></li>";
    echo "<li><p class=\"plisting\">P: $phone</p></li>";
        echo "<li><p class=\"listing\"><span><small>$brands_list</small></span></p></li>";
}
echo "</ul>";
?>

<?php endwhile; ?>

<?php /*Resets current query*/
wp_reset_query(); 
wp_reset_postdata(); 
unset($brands_list);?>

<?php else : ?>

<?php /*If there were no paid dealer and no non-paid dealers we must show a 404 error*/
if ($paidnull=="true")
{
    echo "<h1 class=\"title\">There Are No Dealers Found for your Search</h1>";
    echo "<p style=\"color:#F30\">Use the search below to search a different area</p>";
    echo "<br />";
}?>
<?php endif; ?>
</div>
share|improve this question
    
If you rewrite so that you are not clobbering the main query does it work? –  s_ha_dum Feb 27 '13 at 23:44
    
@s_ha_dum I assume you mean if I leave the original wordpress query in place. It does work in this instance. But that is not an option on this custom taxonomy page. I need a custom query. And when that query actually returns results I don't get the error message. But when the query has no result the error message happens. And that error only happens when the plugin is active. If the plugin is not active no error notice. Any ideas? –  user1609391 Feb 28 '13 at 6:49
add comment

1 Answer

Given that the page works if you leave the original query alone, your solution is pretty simple. Leave the original query alone. You don't need to overwrite that query in order to created secondary loops. Instead of this:

query_single('dealers', 'publish', '1', $taxtype, $value);
if (have_posts()) :
    while (have_posts()) : 
        the_post();

You want:

$myq = new WP_Query($args);
if ($myq->have_posts()) :
    while ($myq->have_posts()) : 
        $myq->the_post();

You can do that as many times as you want on the page and not clobber the main query. The catch is that I don't know what single_query is and so I don't know exactly what $args should look like. Given the way the code works I suspect that single_query runs query_posts, which is strongly discouraged. It could just as easily return a new WP_Query object for you to use, if you tweaked it.

Reference

http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
    
thank you again for the explanation. I tried but still getting the error. Here is what I think is causing the error. In my custom taxonomy all of my posts are set to draft. If the meta field is set to paid then the post will be set to publish. May custom taxonomy page will find this post in this query query_single('dealers', 'publish', '1', $taxtype, $value); which is a new wordpress query not query_posts. The error is not actually happening due to no results in query. The error happens because there are no post in my taxonomy set to published. –  user1609391 Feb 28 '13 at 20:40
    
I also confirmed the above on my main home page. If I have 1 post published the plugin is fine. But if no posts are in my system it errors. Since my custom taxonomy page publishes drafts not published posts I get the error on that page of my site but not other pages of my site. Now I just have to find out where to fix the plugin. –  user1609391 Feb 28 '13 at 20:44
    
It looks like query_single does not return an object and since your code mostly works, does query_single overwrite the global $wp_query variable? –  s_ha_dum Feb 28 '13 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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