WordPress.org

Ready to get started?Download WordPress

Forums

Custom Query for Search (3 posts)

  1. Kwayy Infotech
    Member
    Posted 1 year ago #

    Hello,

    I am creating a new ajax search functionality in my theme.

    I want to search in a Custom Post Type and the search word should be compared with POST ID, POST NAME, POST SLUG, POST CONTENT, POST EXCERPT and also term of a (fixed) texonomy.

    The Custom Post Type is "Property"
    The Texonomy is "Locations"

    So I have created a query:

    SELECT * FROM $wpdb->posts
    	INNER JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    	INNER JOIN $wpdb->terms ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id)
    	WHERE 
    
    	$wpdb->posts.post_status = 'publish'
    	AND $wpdb->posts.post_type = 'properties'
    	AND (
    	$wpdb->terms.name LIKE '%$q%'
    	OR $wpdb->posts.ID LIKE '$q'
    	OR $wpdb->posts.post_content LIKE '%$q%'
    	OR $wpdb->posts.post_title LIKE '%$q%'
    	OR $wpdb->posts.post_excerpt LIKE '%$q%'
    	OR $wpdb->posts.post_name LIKE '%$q%'
    	)
    
    	AND $wpdb->posts.post_status = 'publish'

    This is working but the result is list is repeating properties.
    So anyone help me to correct this query?

  2. Kwayy Infotech
    Member
    Posted 1 year ago #

    The "$q" in the code is the search word

    [ Please do not bump, that's not permitted here. ]

  3. vtxyzzy
    Member
    Posted 1 year ago #

    Have you tried using 'DISTINCT'?

    SELECT DISTINCT * FROM $wpdb->posts

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags