WordPress.org

Ready to get started?Download WordPress

Forums

Display Posts By Category (2 posts)

  1. herold
    Member
    Posted 2 years ago #

    I have the following custom query and I would like to add the functionality to also search by category. Can someone help me with the code to add?

    SELECT $wpdb->posts.* FROM $wpdb->posts
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'page'
    AND $wpdb->posts.post_date > '".$wbc_new_date_start."'
    AND $wpdb->posts.post_date < '".$wbc_new_date_end."'
    ORDER BY $wpdb->posts.post_date ASC
    ";

  2. herold
    Member
    Posted 2 years ago #

    Here is the solution:

    $wbc_query = "
    SELECT $wpdb->posts.* FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.term_id = '".$category_name."'
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_date > '".$wbc_new_date_start."'
    ORDER BY $wpdb->posts.post_date ASC
    ";

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags