Media Categories in WordPress

Hi,

I’m trying to solve an issue with a PHP Wordpress plugin called Media Category Libraries. One of the features of the plugin is that you can access a Media Library through a frontend search feature. You can select media by categories by clicking checkboxes. If you select multiple checkboxes however, it returns all media belonging to all the checked categories, not just media that fits all the categories selected. To explain better:

Media 1 categories=“Cats”
Media 2 categories=“Dogs”
Media 3 categories=“Dogs, Cats”

If I ticked Cats and Dogs, I would only want it to find Media 3, and ignore the other two.

I believe the issue may be in this piece of php here

[php]if ( count( $selected_terms ) > 0 ) {

		// create sub query

		$sub_sql = "SELECT x.term_taxonomy_id FROM " . $wpdb->term_taxonomy . " AS x " .
				"LEFT JOIN " . $wpdb->terms . " AS t ON x.term_id = t.term_id WHERE " .
				"t.slug IN(" . implode( ",", $selected_terms ) . ")";

		// main query that uses subquery

		$sql = "SELECT SQL_CALC_FOUND_ROWS DISTINCT p.ID, p.post_title, p.post_mime_type, p.post_excerpt, p.post_date FROM " . $wpdb->posts . " AS p " . "LEFT JOIN " . $wpdb->term_relationships . " AS r ON p.ID = r.object_id " .
				"WHERE r.term_taxonomy_id IN($sub_sql) AND p.post_type='attachment' ";

[/php]

Any help on this would be fantastic thanks :slight_smile:

Jake

Depending on the version of mysql you are using you can try this, without getting overly completed.

[php]$sub_sql = "SELECT x.term_taxonomy_id FROM " . $wpdb->term_taxonomy . " AS x " .
"LEFT JOIN " . $wpdb->terms . " AS t ON x.term_id = t.term_id WHERE " .
"t.slug REGEX ’ " . implode( “|”, $selected_terms ) . “’”;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service