WordPress.org

Support

Support » Plugins and Hacks » Custom Select Query: Get posts by author meta

Custom Select Query: Get posts by author meta

  • Scott Fennell

    @scofennellgmailcom

    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

Viewing 1 replies (of 1 total)
  • Scott Fennell

    @scofennellgmailcom

    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.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Select Query: Get posts by author meta’ is closed to new replies.
Skip to toolbar