WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Why is the value of this variable changing? (21 posts)

  1. ryanoz
    Member
    Posted 7 years ago #

    the PHP:

    <?php
    $post = $posts[0]; // Hack
    $likedate = 'Y-m-d ';
    
    $count = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_date LIKE '" . get_the_time($likedate) . "%' AND post_date_gmt < '" . current_time('mysql', TRUE) . "' AND post_status = 'publish'");
    echo $count;
    ?>

    the above PHP is in my sidebar.php
    here the value of $count is 3:
    http://www.oswaldtrading.com/testing/

    here the value of $count is 1:
    http://oswaldtrading.com/testing/category-index/

    you can see it echoed on the pages.
    Why is it changing from 3 to 1 ?

    Thanks for looking.

  2. yoshi
    Member
    Posted 7 years ago #

    Echo the query. They must be different.

    get_the_time() returns the timestamp of the "current post".

    I can only guess but the first page looks like a "Page" page, so the time "category-index" page published is used. If no other post/page is published on the same day, only the page itself will be counted. $count = 1.

    In the second page, $post will be the last post called in the loop(titled "eruditi sed in..."). Obviously there are 3 posts published on the same date. $count = 3.

  3. ryanoz
    Member
    Posted 7 years ago #

    yea "category-index" is the empty PAGE I published in order to create a category index. Why is the published PAGE counted as POST?

    I don't want the 3 entries under "previously" to change. How do I keep the query the same?

    This is what's in my sidebar:

    <?php
    $post = $posts[0]; // Hack
    $likedate = 'Y-m-d ';
    
    $count = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_date LIKE '" . get_the_time($likedate) . "%' AND post_date_gmt < '" . current_time('mysql', TRUE) . "' AND post_status = 'publish'");
    echo $count;
    ?>
    <?php query_posts("showposts=3&offset=$count"); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><span><?php the_time('F jS, Y') ?> - <?php wp_link_pages(); ?> <?php comments_number(__('<strong>0</strong> Comments'), __('<strong>1</strong> Comment'), __('<strong>%</strong> Comments')); ?></span></a></li>
    <?php endwhile; else: ?>
    <?php endif; ?>

    Thanks

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Why is the published PAGE counted as POST?

    Pages are stored in the posts table too. They have timestamps and everything else. So do drafts and attachments, for that matter.

    A Page is just a Post with special features. It has a fixed base URL and the option to have it's own template, but basically it's the same sort of thing.

    If you only want Posts, you need to add "AND post_type='post'" to your where clause.

    And yes, get_the_time() is going to return the Page's timestamp as well. Pages are run through the Loop like everything else is.

  5. rudolf45
    Member
    Posted 7 years ago #

    1. You don't need a "category-index" to list posts in a catgeory. Category Templates just do that for you out of the box.
    2. Your code is wrong for the 2.1 branch. I have found this: http://wordpress.org/support/topic/102028

  6. ryanoz
    Member
    Posted 7 years ago #

    otto-

    If you only want Posts, you need to add "AND post_type='post'" to your where clause.

    Ok, I added that.
    If you look at: http://oswaldtrading.com/testing/category-index/
    you'll notice in the sidebar "previously" the $count echo
    is showing 0 now because their are no post ON THAT PAGE.
    But I want the the entries under "previously" to be the
    same 3 as on the index page. How do I do that?

    rudolf45

    1. You don't need a "category-index" to list posts in a catgeory. Category Templates just do that for you out of the box.

    I just wanted to list the categories, not their posts.

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Try something for me:
    echo get_the_time($likedate);

    What's the date you get for the Page vs. the date you get for the posts?

  8. ryanoz
    Member
    Posted 7 years ago #

    Otto-

    Try something for me:
    echo get_the_time($likedate);

    Ok. On my index page it echo's: 2007-04-24
    On the category-index it echo's: 2007-04-25

    You can see it on the two links i've listed above.
    The first number echo'd is the value of $count.

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Well, there you go then. Look at your query:

    SELECT COUNT(ID) FROM $wpdb->posts WHERE post_date LIKE '2007-04-25%'...

    Do you *have* any posts made on the 25th?

  10. ryanoz
    Member
    Posted 7 years ago #

    Do you *have* any posts made on the 25th?

    No. But why is it returning 2007-04-24 on the index page
    and 2007-04-25 on the category-index?

    Do you see what I want to happen?

    I want the same 3 posts listed under "previously", I don't want that to change at all.

  11. ryanoz
    Member
    Posted 7 years ago #

    Is it showing 2007-04-25 because that's the date that I created the category-index PAGE? If so, how would you change the date the page was created?

  12. ryanoz
    Member
    Posted 7 years ago #

    Any ideas here? I don't have a clue with this PHP stuff.
    I just copy and paste bits and pieces I find from support topics or the docs that do what I need to do.
    So if I don't find answers here, then that's it. I'm a designer, not a database programmer.

    I just want the category sidebar to look exactly like the index sidebar.

  13. ryanoz
    Member
    Posted 7 years ago #

    I'm in the wordpress IRC channel if any of you smart guys would like to help me out with this in real-time :)

    irc.freenode.net

    /join #wpmu

    my nick is ryanoz

    as you can see, i'm getting desperate

  14. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    We don't read this forum moment by moment. It's not like we get paid to help you, you know. So don't expect a quick response, please.

    Is it showing 2007-04-25 because that's the date that I created the category-index PAGE?

    Yep.

    If so, how would you change the date the page was created?

    Edit the database, change the post_date and post_date_gmt for your page's post.

  15. ryanoz
    Member
    Posted 7 years ago #

    We don't read this forum moment by moment. It's not like we get paid to help you, you know. So don't expect a quick response, please.

    I realize this, believe me. I at least wait 30 mins :) Surely you understand like all of us how frustrating it can be when your stuck, and all you can do is sit and wait for a response on this forum; not knowing if it will be 5 mins. or 5 days.

    And I'd be happy to pay you or anyone that can solve any issue I have, seriously. In fact, I would rather it be that way. If your interested, let me know.

    Edit the database, change the post_date and post_date_gmt for your page's post.

    uhhh, apparently you missed my whole "i'm not a database programmer" comment. I have no idea how to modify the database. However, I am looking into this plugin as a solution... would you know how to change the PAGE's date with this:
    http://guff.szub.net/2005/02/22/post-updated/

  16. yoshi
    Member
    Posted 7 years ago #

    ryanoz,
    Can you discribe what exactly what you want under "previously" rather than let us guess and confuse you more?

  17. ryanoz
    Member
    Posted 7 years ago #

    sure. Although, I think I understand what is going on.
    But let me describe in detail.
    Under "previously" is 3 entries made PRIOR to the last posting date.

    So you can see: http://www.oswaldtrading.com/testing/
    the main post "... 19" is on 4-24
    then I have under that two more post from 4-24 "...18" "...17". So these 3 post were all on 4-24.

    Then under "previously" is 3 post made prior to 4-24.

    What I think is happening is...
    I created the category-index page on 4-25, so my query is returning 4-25, and I have no posts on that date. So I think by tomorrow, everything will work itself out, I'm hoping at least.

    The way I have it set up is I have wordpress set to only show 1 post. Then I scratched together some PHP that checks to see if there are any more post from the same day as the last post, if there is it will show them under "Earlier..." with an excerpt.

    Then in my sidebar It get the total number of posts made on the last date there is a post and sets that number as the offset and displays 3 posts prior to that date. Make sense?

    I just think its not working correctly right now because I created the category-index PAGE on 4-25. If I change the date of the last 3 posts to 4-25, then everything works how it should.

    Although, it may be that I need to change the date of category-index page. Which I don't know how to do. Any ideas on that?

    Thanks for looking. Sorry if this is long and confusing.

  18. yoshi
    Member
    Posted 7 years ago #

    Thanks for explaining. Now I got it.

    I just think its not working correctly right now because I created the category-index PAGE on 4-25. If I change the date of the last 3 posts to 4-25, then everything works how it should.

    For now, but not in the future. If you add new post on 27th April, get_the_time($likedate) in category-index still will be '2007-04-25'. You will have to keep changing the date of category-index page everytime you post a new article.

    Instead of using get_the_time($likedate), you can get the number of posts on the last posting date like this:

    $count = $wpdb->get_var("SELECT count(ID) as post_count
               FROM $wpdb->posts
               WHERE post_status = 'publish' AND post_type='post'
               GROUP BY YEAR(post_date), MONTH(post_date), DAY(post_date)
               ORDER BY post_date DESC LIMIT 1");

    This gives you the same result anywhere in the site. Date of the Page will not affect.

  19. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Nevermind, yoshi answered it better than me. :)

  20. ryanoz
    Member
    Posted 7 years ago #

    yoshi-
    Thank you so much, that works perfect.
    This is much more efficient and actually makes sense when you look at it. I'll have do some reading on the $wpdb-> thing, cause it looks like I could learn some things.
    Thanks for taking the time to read, understand, and help me.

    Thanks to Otto and rudolf also for you help

  21. yoshi
    Member
    Posted 7 years ago #

    Grad you got it worked!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.