• Resolved lsshock

    (@lsshock)


    Hi everyone, i was wondering how i could do the following.

    in my sidebar only show the posts of this week (im doing 1 each day)
    so dont show the last 7 but for example to day it would only be 1, or on a thursday it would be 4

    thanks in advance

Viewing 15 replies - 1 through 15 (of 15 total)
  • Try querying it out of WordPress from MySQL.

    <?php 
    
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    
    $query = mysql_query("SELECT * FROM [prefix_]posts ORDER BY post_date DESC LIMIT 0,7");
    while($r = mysql_fetch_array($query)) {
    $postdate = $r["post_date"];
    $unixPostdate = strtotime($postdate);
    $mondaydate = strtotime('Monday 01:00:00');
    if(date('D')=="Mon") $mondaydate = strtotime('Today 01:00:00');
    if($unixPostdate>$mondaydate) {
        echo "<a href=\"".$r["guid"]."\">".$r["post_title"]."</a>";
    }; ?>

    I haven’t tested it. You can edit the output in the line starting echo .... Let me know if it works, or if not, what errors you get.

    Thread Starter lsshock

    (@lsshock)

    thanks for the fast reply
    i get an error after filling in the DB info

    Parse error: syntax error, unexpected $end in /home/rick/domains/ideaprison.com/public_html/dailystory/wp-content/themes/ideaprison/sidebar.php on line 50

    and inside that file line 50 is actually blank..

    Did you adjust [prefix_] to the value of $table_prefix in wp-config?

    Also, I’ve just changed some of that code. Try it again with the new code which has removed some bugs.

    Thread Starter lsshock

    (@lsshock)

    did not see that one, i filled in the prefix wp_ assuming thats what it needs.

    still gives me the same error but now line 49

    sorry if im really dumb šŸ˜‰

    *i tried with the new code but still wont fire

    Basically it means that something hasn’t closed correctly – ie. missing a ” ‘ } ) ; or too many { ( ‘ “.

    Did you insert this over any old PHP?

    Can I see your entire sidebar code with any changes (except hash your mysql_host and password)?

    Got it! Needs another } just before ?>, so:

    <?php 
    
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    
    $query = mysql_query("SELECT * FROM [prefix_]posts ORDER BY post_date DESC LIMIT 0,7");
    while($r = mysql_fetch_array($query)) {
    $postdate = $r["post_date"];
    $unixPostdate = strtotime($postdate);
    $mondaydate = strtotime('Monday 01:00:00');
    if(date('D')=="Mon") $mondaydate = strtotime('Today 01:00:00');
    if($unixPostdate>$mondaydate) {
        echo "<a href=\"".$r["guid"]."\">".$r["post_title"]."</a>";
    }; }; ?>
    Thread Starter lsshock

    (@lsshock)

    new error
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    my line: $query = mysql_query(“SELECT * FROM [wp_]posts ORDER BY post_date DESC LIMIT 0,7”);

    from my config file
    $table_prefix = ‘wp_’;

    Remove the [ and the ] so is wp_posts. Also drop the 01:00:00 bit.

    Mine works in that I get nothing (having written nothing today).

    Thread Starter lsshock

    (@lsshock)

    oh thats still dumb of me
    it works! thanks for the help and writing such nifty code!

    Thread Starter lsshock

    (@lsshock)

    on small thing though is it also possible to have it use the permalink of a post?

    Ok, code has been reworked to get rid of some bugs you might come across over time. You must go to this version!

    <?php 
    
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    
    $query = mysql_query("SELECT * FROM [prefix_]posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC LIMIT 0,7");
    while($r = mysql_fetch_array($query)) {
    $postdate = $r["post_date"];
    $unixPostdate = strtotime($postdate);
    $mondaydate = strtotime('Monday');
    if(date('D')=="Mon") $mondaydate = strtotime('Today');
    if($unixPostdate>$mondaydate) {
        echo "<a href=\"".$r["guid"]."\">".$r["post_title"]."</a>";
    }; }; ?>

    Replace [prefix_] with your database prefix (wp_ by default) and enter your database details in to the first two lines (eg mysql_select_db("mydbname");).

    To get the permalink use echo "<a href=\"". get_permalink($r["ID"]) ."\">".$r["post_title"]."</a>"; in place of the old echo.

    When I get round to it I shall turn this in to a plugin, and post here with its url. I’m thinking it would be nice to be able to set this to only one filter one category (or two, or even more) and allow the user to start it from any day.

    PS. What’s your URL?

    Thread Starter lsshock

    (@lsshock)

    This works much better, the other did indeed gave me a bug of double posts.

    Thread Starter lsshock

    (@lsshock)

    It doesnt show the post of yesterday in the sidebar right now
    i emailed you the url to the test site

    Hmmm, try

    <?php 
    
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    
    $query = mysql_query("SELECT * FROM [prefix_]posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC LIMIT 0,7");
    while($r = mysql_fetch_array($query)) {
    $postdate = $r["post_date"];
    $unixPostdate = strtotime($postdate);
    $mondaydate = strtotime('Last Monday');
    if(date('D')=="Mon") $mondaydate = strtotime('Today');
    if($unixPostdate>$mondaydate) {
        echo "<a href=\"".$r["guid"]."\">".$r["post_title"]."</a>";
    }; }; ?>

    The change is in the first creation of $mondaydate where we call ‘Last Monday’ instead of ‘Monday’.

    Thread Starter lsshock

    (@lsshock)

    thanks Ben!
    it works now it should until next week.
    i mean show the posts of this week

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Sidebar show posts of this week only’ is closed to new replies.