Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#37042 new feature request

Additional filter for has_category function

Reported by: jakubbis's profile jakubbis Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5.2
Component: Taxonomy Keywords: reporter-feedback
Focuses: Cc:

Description

Our plugin translates categories and posts. Let's assume we have two languages:

  • original: English
  • second: French

We defined category "test" and its translation "test-fr". The category is assigned to the post in such way that English post has English category and vice versa.

Currently, when we call has_category("test") in single.php file, it returns true only for the English post ( because French one does not have "test" category but "test-fr" ). We expect that result will be true in both cases.


For this reason, we need some filter inside is_object_in_term function, similar to "get_object_terms" inside "wp_get_object_terms" function. It will give as opportunity to adjust "has_category" behaviour to out requirements.

Change History (2)

#1 @jakubbis
8 years ago

We can obtain this goal e.g. in this way:

<?php
function has_term( $term = '', $taxonomy = '', $post = null ) {
        $default_args = array( $term, $taxonomy, $post );
        $args         = apply_filters( 'has_term', array( $term, $taxonomy, $post ) );

        $args = array_merge( $default_args, $args );

        $term     = $args[0];
        $taxonomy = $args[1];
        $post     = $args[2];
        
        $post = get_post($post);

        if ( !$post )
                return false;

        $r = is_object_in_term( $post->ID, $taxonomy, $term );
        if ( is_wp_error( $r ) )
                return false;

        return $r;
}

has_term filter will allow to transform $term value before proceeding actual logic.

#2 @boonebgorges
8 years ago

  • Component changed from General to Taxonomy
  • Keywords reporter-feedback added

Hi @jakubbis -

The filter you suggested looks strange to me - it doesn't allow you to filter 'has_term' so much as it allows you to fake the parameters that are passed to has_term(). This is not a pattern we often use in WP filters.

I think that what you're trying to do can probably already be done using the get_object_terms array:

function wp37042_match_tags( $terms, $object_ids, $taxonomies, $args ) {
    $all_terms = array( $terms );
    foreach ( $terms as $term ) {
        // Assuming `$term` is an object - you would probably need more checks for this
        $the_term_in_other_languages = whatever();
        $all_terms = array_merge( $all_terms, $the_term_in_other_languages );
    }

    return $all_terms;
}
add_filter( 'get_object_terms', 'wp37042_match_tags', 10, 4 );

In other words: if a term has 'test', get the term corresponding to 'test' in all other languages ('test-fr', etc) and add it to the array of terms that belong to the object.

Does that seem right to you?

If you still think it's necessary and/or easier to have a more specific filter, is_object_in_term() seems like a more appropriate place for it.

Note: See TracTickets for help on using tickets.