How to search from specific post type with tags?

The way you are customizing the query you are telling it to search for posts that ALSO match a custom tax_query criteria. This would never work, logically speaking.

You said, “I want it to return a result if this tag is searched or if it matches the title” for which I understand ONE or ANOTHER.

I’ll show you a completely different approach, using basically the following logic:

  1. You said “This post type has a title and a video id” so I assume you are not going to use the field/column post_excerpt for the CPT commercials. You can add it to supports, but it is not required for this to work.
  2. We can make use of the action save_post to force WordPress adding your post tags to the field post_excerpt of a Commercial. When you take a look at the code this will sound less confusing.
  3. We know that WordPress by default searches the columns post_title, post_excerpt and post_content for a term when the query var s is present. So doing number 2 above would solve your issue, because WordPress would take care of the rest.

Getting our hands dirty… we will still need to use the pre_get_posts action (yes, not a filter):

/**
 * @param WP_Query $query
 */
function searchCommercial( $query ) {
    if ( is_search() && ! is_admin() ) {
        if ( isset( $_GET['post_type'] ) ) {
            $type = $_GET['post_type'];
            if ( $type == 'commercials' ) {
                $query->set( 'post_type', array( 'commercials' ) );
            }
        }
    }
}

add_action( 'pre_get_posts', 'searchCommercial' );

And for saving the post tags within the post_excerpt of a Commercial:

/**
 * Fires once a post has been created or updated.
 *
 * @param int     $post_id
 * @param WP_Post $post
 * @param bool    $update Whether this is an existing post being updated or not.
 */
function add_post_tags_to_commercials( $post_id, $post, $update ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    if ( $post->post_type != 'commercials' ) {
        return;
    }

    $post_tags = wp_get_object_terms( $post_id, 'post_tag' );

    if ( $post_tags ) {
        $tags_with_comma = [];

        /**
         * @var WP_Term $tag
         */
        foreach ( $post_tags as $tag ) {
            $tags_with_comma[] = $tag->name;
            $tags_with_comma[] = $tag->slug;
        }

        $tags_with_comma = implode( ',', $tags_with_comma );

        // Unhook this function so it doesn't loop infinitely.
        remove_action( 'save_post', 'add_post_tags_to_commercials', 20 );

        wp_update_post(
            array(
                'ID'           => $post_id,
                'post_excerpt' => $tags_with_comma, // The tags will be saved within the `post_excerpt` column.
            )
        );

        // Re-hook this function.
        add_action( 'save_post', 'add_post_tags_to_commercials', 20, 3 );
    }
}

add_action( 'save_post', 'add_post_tags_to_commercials', 20, 3 );

The 2 snippets above can be placed within your functions.php file.

Notice:

  • If you are using Guttenberg for your CPT commercials, you may have issues attaching post tags to it. Until now, WP version 5.2.1, is still having trouble attaching tags to a post using the AJAX process.
  • You have to save all of your Commercials again. I hope it’s just a few.
  • Consider adding the string excerpt to your commercials supports as it would make it easier for you to see what’s been added to the post_excerpt column:
'supports'           => array( 'title', 'thumbnail', `excerpt` ),

Leave a Comment