Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // add filters for specific tags to the widget and shortcode
- add_filter( 'related_posts_by_taxonomy_widget_args', 'related_posts_filters' );
- add_filter( 'related_posts_by_taxonomy_shortcode_atts', 'related_posts_filters' );
- function related_posts_filters( $instance ) {
- // add filter to wp_get_object_terms
- add_filter( 'wp_get_object_terms', 'specific_related_tags', 10, 4 );
- // remove filter after the tags are filtered
- add_filter( 'related_posts_by_taxonomy', 'remove_related_posts_filters', 10, 4 );
- return $instance;
- }
- function specific_related_tags( $terms, $object_ids, $taxonomies, $args ) {
- // term ids you want to include
- $include_terms = array( 55, 34, 40 );
- if ( !empty( $terms ) ) {
- foreach ( $terms as $key => $term ) {
- if ( !in_array( $term, $include_terms ) )
- unset( $terms[ $key ] );
- }
- }
- return array_values( $terms );
- }
- function remove_related_posts_filters( $results, $post_id, $taxonomies, $args ) {
- // remove filters after the related posts are retrieved from the database
- remove_filter( 'related_posts_by_taxonomy', 'remove_related_posts_filters', 10, 4 );
- remove_filter( 'wp_get_object_terms', 'specific_related_tags', 10, 4 );
- return $results;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement