12 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
DigitalFame says

Hi there!

I want to list images from nextgen gallery using tags. Which is not a big deal BUT in this case I need images belonging to multiple tags:

pseudo example: get_objects_in_term_intersection(array(“141”,”144”), “ngg_tag”);

So I only need objects which belong to BOTH 141 and 144 (or any number of tags passed through the array). Nextgen uses `get_objects_in_term()` from `wp-includes/taxonomy.php` from line 494. I guess I should change the sql query in the function but this is way out of my league.

The original function:
 function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
    global $wpdb;

    if ( ! is_array( $term_ids ) )
        $term_ids = array( $term_ids );

    if ( ! is_array( $taxonomies ) )
        $taxonomies = array( $taxonomies );

    foreach ( (array) $taxonomies as $taxonomy ) {
        if ( ! taxonomy_exists( $taxonomy ) )
            return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    }

    $defaults = array( 'order' => 'ASC' );
    $args = wp_parse_args( $args, $defaults );
    extract( $args, EXTR_SKIP );

    $order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';

    $term_ids = array_map('intval', $term_ids );

    $taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
    $term_ids = "'" . implode( "', '", $term_ids ) . "'";

    $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");

    if ( ! $object_ids )
        return array();

    return $object_ids;
    }

Thnx in advance!

by
by
by
by
by
by