I have been trying to achieve this for the past two days but nothing works. I am attempting to build functionality to search any post, page, and a couple other custom post-types by a tag. I have created a plugin which registers a taxonomy called search_tag.


require_once __DIR__ . '/includes/search-tags.class.php';

function search_tags_closure() {
    $search_tags = new Search_Tags;


add_action('init', 'search_tags_closure');


class Search_Tags
    const NAME = 'search_tag';

    const LABEL = 'Search Tags';

    protected function get_post_types()
        $args = [
            'public'             => 'true',
            'publicly_queryable' => 'true',

        $post_types = get_post_types($args, 'names');
        $post_types['page'] = 'page';


        return array_keys($post_types);

    protected function is_custom_post_type($name)
        $built_in_post_types = [

        if (in_array($name, $built_in_post_types)) return false;

        return true;

    public function create_taxonomy()
        $name = $this::LABEL;

        $labels = [
            'name'                => $name,
            'menu_name'           => $name,
            'singular_name'       => 'Search Tag',
            'all_items'           => "All $name",
            'edit_item'           => "Edit $name",
            'view_item'           => "View $name",
            'update_item'         => "Update $name",
            'add_new_item'        => "Add New $name",
            'new_item_name'       => "New $name",
            'search_items'        => "Find $name",
            'add_or_remove_items' => "Add or remove $name",

        $args = [
           'labels'           => $labels,
           'show_in_nav_menu' => false,

        register_taxonomy($this::NAME, $this->get_post_types(), $args);

        foreach ($this->get_post_types() as $post_type) {
            register_taxonomy_for_object_type($this::NAME, $post_type);

Everything above works fine. The taxonomy is registered and I have added tags to a page.

Below is the code I have to query pages by the search_tag taxonomy. I have hard coded a value for the tax_query terms temporarily to test it. I am sure the term and taxonomy exists as they are shown when I list the term and taxonomy using the WP CLI.


function testing() {
    $args = [
        'post_type' => [
        'numberposts' => -1,
        'post_status' => 'publish',
        'tax_query' => [
                 'taxonomy' => 'search_tag',
                 'field'    => 'name',
                 'terms'    => 'Nothing',

    return get_posts($args);


Does your custom post type have the setting publicly_queryable=true?

Check the post type in the resulting query to see if it matches your post type. I'm guessing that your post type 'distribution_centre' is not being queried for one reason or another.

add_filter( 'posts_request', 'dump_request' );//show the query

function dump_request( $query ) {
    echo "<pre>";
    var_dump( $query );
    echo "</pre>";
    return $query;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.