ThemeForest

Weird SQL Loop issue in Wordpress

242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says

OK, I am trying to develop a Wordpress template that has built in popular post functionality. Everything works, except for one section.

When a single post is viewed, I have the following code in the single.php file.

get_header();

if (have_posts()) :
    while (have_posts()) : the_post();
        $popular_post_id = $post->ID;
        global $wpdb;

        $cm_popular_post_insert = $wpdb->query("UPDATE wp_cm_popular_post SET last_viewed=NOW(), page_views = page_views + 1 WHERE postID=$popular_post_id");

The table is being updated as it should, but what the issues is, is the current post being viewed is not the only postID that get updated. It always updates a second postID along side the correct one. I did some debugging. and figure out the problem lies with wp_head(). If I remove that, everything works as expected.

Does anyone have any theiories on why this weird loop issue is occuring?

2 years ago
6 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Europe
iMarcus says
Seems the loop is pulling more than 1 post, you could try using: query_posts('showposts=1'); before the while(have_posts()); to lock it down to just 1 post, strange that you’d need to on a single page though?
get_header();

if (have_posts()) :
 query_posts('showposts=1');
    while (have_posts()) : the_post();
        $popular_post_id = $post->ID;
        global $wpdb;

        $cm_popular_post_insert = $wpdb->query("UPDATE wp_cm_popular_post SET last_viewed=NOW(), page_views = page_views + 1 WHERE postID=$popular_post_id");

...after the endwhile; use wp_reset_query(); to prevent screw ups elsewhere on the page, learnt that the hard way :(

2 years ago
1842 posts
  • Has been a member for 3-4 years
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 10 and 49 items
  • Europe
  • Referred between 100 and 199 users
digitalimpact says
...after the endwhile; use wp_reset_query(); to prevent screw ups elsewhere on the page, learnt that the hard way :(

So true.

OFF : did it ever happen that comments get screwed up if you don’t use the query reset? Like a user comments on post1, but the comment is assigned to a different post in the db?

2 years ago
242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says
Seems the loop is pulling more than 1 post, you could try using: query_posts('showposts=1'); before the while(have_posts()); to lock it down to just 1 post, strange that you’d need to on a single page though?
get_header();

if (have_posts()) :
 query_posts('showposts=1');
    while (have_posts()) : the_post();
        $popular_post_id = $post->ID;
        global $wpdb;

        $cm_popular_post_insert = $wpdb->query("UPDATE wp_cm_popular_post SET last_viewed=NOW(), page_views = page_views + 1 WHERE postID=$popular_post_id");
...after the endwhile; use wp_reset_query(); to prevent screw ups elsewhere on the page, learnt that the hard way :(

I added the query_posts(showpost=1) and now I am seeing a different posts information on the single post page. i.e. the permalink is for post 1 but the content displayed (title, content, ...) is for post post 2. Weird!

2 years ago
6 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Europe
iMarcus says

how many posts are there, if its just 2 then perhaps you need to add &order=DESC to the query, otherwise im not sure?

2 years ago
242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says

I think I found a bug in Wordpress 2.9, because I reverted back to the “Default” Wordpress theme and just transferred over nothing but my query, and the problem is still occurring.

2 years ago
510 posts
  • Has been a member for 3-4 years
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Exclusive Author
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Power Elite Author
  • Bought between 50 and 99 items
  • Austria
  • Referred more than 2000 users
Kriesi says

if you use “query_posts(showposts=1)” you will query one post: the latest one that is found within your database no matter which URL you are currently viewing

you need to preserve the old query as well when using query_posts to make sure you get the one post from the page you are currently viewing: (but keep in mind that this will retrieve the latest post of this section, so if you are viewing a category overview page it will retrieve the latest post from this category)

query_posts($query_string . "showposts=1");

Besides that if your code works once you delete the wp_head() it could very well be that a plugin is causing your error, since plugins often use the wp_head to hook into the theme…

2 years ago
242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says
if you use “query_posts(showposts=1)” you will query one post: the latest one that is found within your database no matter which URL you are currently viewing

you need to preserve the old query as well when using query_posts to make sure you get the one post from the page you are currently viewing: (but keep in mind that this will retrieve the latest post of this section, so if you are viewing a category overview page it will retrieve the latest post from this category)

query_posts($query_string . "showposts=1"); Besides that if your code works once you delete the wp_head() it could very well be that a plugin is causing your error, since plugins often use the wp_head to hook into the theme…

Thanks Kriesi, I actually noticed that also. I also just discovered is that the unintented updated postID is always the next post according to ID. So if I am viewing post 109, post 109 and 110 will both get updated in that query. I am totally stumped on this. I think it is a Wordpress 2.9 bug.

2 years ago
8 posts
  • Has been a member for 3-4 years
  • Exclusive Author
  • Bought between 10 and 49 items
  • United States
codyrobert says

I added your update query, along with a sample table to update, to a fresh installation of wordpress i have. No plugins, and bug-free theme. It worked perfectly as it should have.

Like Kreisi said, it’s very likely a plugin conflict.

2 years ago
242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says

The weird thing, is I am not using any plugins. I even deleted the default plugins.

2 years ago
by
by
by
by
by