Support » Fixing WordPress » Display Posts By Category

  • 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
    “;

Viewing 1 replies (of 1 total)
  • 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
    “;

Viewing 1 replies (of 1 total)
  • The topic ‘Display Posts By Category’ is closed to new replies.