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

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!

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes