SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question My wp tweaks are causing a mysql error...

    I'm using a wordpress custom theme that has this piece of code to prune posts after a number of days (prun_period).

    I'm trying to change this to instead prune posts on a certain date. I've created a custom meta field for the expiration date called 'e_date'.

    The original code works as intended and looks like this:

    Code:
    if (get_option("post_prun") == "yes" && get_option("prun_period") != "" && get_option("post_prun") != "") {
    
    	$prun_period = get_option("prun_period");
    
    	$sql = "SELECT `ID` FROM $wpdb->posts WHERE `post_date`<'".date('Y-m-d h:i:s', strtotime("-$prun_period days"))."' AND `post_status`='publish' AND `post_type`='post' LIMIT 10";
    
    	$sql = mysql_query($sql);
    
    	while ($row=mysql_fetch_array($sql)){
    
    		$post_id = (int)$row['ID'];
    
    
    
    		if (get_option("prun_status") == "1") {
    
    			$my_post = array();
    
    			$my_post['ID'] = $post_id;
    
    			$my_post['post_status'] = 'draft';
    
    			wp_update_post( $my_post );
    
    		} else if (get_option("prun_status") == "2") {
    
    			wp_delete_post($post_id);
    
    		}
    
    	}
    
    }
    
    ?>
    I thought I might be able to just change post_date to e_date and then either delete the part where it subtracts the prun_period or else set it to zero, but it gives me this error.

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
    I assume it's because event_date is a meta field rather than whatever post_date is, but I'm not sure how to fix.

    Any help?

    Thanks in advance.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    that error is a php error

    to see the real mysql error, echo your query and run it directly in mysql, not via php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I have no idea what that even means.

    How do I do that?

    At any rate, I really don't need to see the error. What I'm really asking is how to tweak the original code that works to do what I want.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    i don't do php, but i think the command is <?php echo($sql) ?>

    and yes, you do want to see the error, because in order to fix it, you need to know what's causing it
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I appreciate the help. What I meant was I don't really need to fix the error. The error was caused by something I tried that was obviously wrong. I probably shouldn't have even included it in the post at all. I was just mentioning what I had already tried.

    What I'm asking is if someone can tell me how to tweak that original code to prune on the e_date instead of after prun_period.

  6. #6
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?

  7. #7
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure if i'm right, but u could try replacing
    post_date<.date('Y-m-d h:i:s', strtotime("-$prun_period days")). blah blah
    with just
    post_date=.e_date. blah blah..
    do take note of the quotes, i'd removed here to make it look cleaner. u'll have to add it back.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •