WordPress.org

Ready to get started?Download WordPress

Forums

Custom Select Query: Get posts by author meta (2 posts)

  1. Scott Fennell
    Member
    Posted 2 years ago #

    Where am I going wrong here:

    I'm trying to allow the user to search for posts by a usermeta, in this case what city the author has entered. The city is stored in usermeta under the key "public_city".

    if($search_select=='city'){
    $querystr="
    SELECT DISTINCT wposts.*, wusermeta.meta_value
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->usermeta wusermeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wposts.post_author = wusermeta.user_id
    AND wusermeta.meta_key = 'public_city'
    AND wposts.post_type = '$page_template'
    AND wposts.post_status = 'publish'
    AND wposts.post_date < NOW()
    AND ( wposts.post_title LIKE '%$search_text%' OR wposts.post_content LIKE '%$search_text%' )
    ";
    if(isset($paid)){
    $querystr .= "
    AND wpostmeta.meta_key = 'interns_paid'
    AND wpostmeta.meta_value = 'on'
    ";
    }
    $querystr .= "
    ORDER BY wposts.post_date DESC
    ";

    This is what $querystr ends up being:

    SELECT DISTINCT wposts.*, wusermeta.meta_value
    FROM wp_posts wposts, wp_postmeta wpostmeta, wp_usermeta wusermeta
    WHERE wposts.ID = wpostmeta.post_id AND wposts.post_author = wusermeta.user_id AND wusermeta.meta_key = 'public_city'
    AND wposts.post_type = 'intern'
    AND wposts.post_status = 'publish'
    AND wposts.post_date < NOW()
    AND ( wposts.post_title LIKE '%Seattle%'
    OR wposts.post_content LIKE '%Seattle%' )
    ORDER BY wposts.post_date DESC
  2. Scott Fennell
    Member
    Posted 2 years ago #

    Just to clarify, there is a conditional in there to add some AND clauses in case the user is searching for a particlar postmeta value, but that's not what's tripping me up.

Topic Closed

This topic has been closed to new replies.

About this Topic