php & msql: help needed: to query wheter post_title!=("*favourite*)

Discussion in 'PHP' started by kotaro, Apr 30, 2009.

  1. #1
    hi.

    i need help. i want to delete posts where its title != *favourite*

    e.g. if title does not contain the word favourite, then it will de deleted.


    function deleteNoMatchPosts(){
    global $wpdb;
    $wpdb->query(" DELETE FROM $wpdb->posts WHERE post_title!='*favourite*' ");
    }

    add_action('publish_post', 'deleteNoMatchPosts');



    is it correct?
     
    kotaro, Apr 30, 2009 IP
  2. mulari

    mulari Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    != is not used in mysql.

    DELETE FROM $wpdb->posts WHERE post_title NOT LIKE '%favourite%' "


    % is used in sql as a wildcard char, so it should be used if you meant * as a wildcard.

    if you meant the actual * char, it should be like this:
    DELETE FROM $wpdb->posts WHERE post_title NOT LIKE '*favourite*' "
     
    mulari, May 1, 2009 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    Not strictly true. In this instance it wouldn't work since a LIKE is being used, however it is valid in mysql to use != in the format:

    SELECT field1 FROM table WHERE field1 != 'value'
     
    mfscripts, May 1, 2009 IP
  4. kotaro

    kotaro Peon

    Messages:
    113
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    THANK YOU very much.

    now i want to extend it..

    DELETE * FROM wp_posts WHERE post_date > '2009-05-01 00:00:00' AND post_title NOT LIKE %favourite%'

    doesnt work. please somebody fix it
     
    kotaro, May 1, 2009 IP
  5. kotaro

    kotaro Peon

    Messages:
    113
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    DELETE * FROM wp_posts WHERE post_date > '2009-05-01 00:00:00' AND FROM wp_posts WHERE post_title NOT LIKE %favourite%'

    doesnt work too
     
    kotaro, May 1, 2009 IP
  6. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #6
    Looks like you are missing a single quote at the start of %favourite%:

    DELETE * FROM wp_posts WHERE post_date > '2009-05-01 00:00:00' AND post_title NOT LIKE '%favourite%'
     
    mfscripts, May 1, 2009 IP