Site icon Hip-Hop Website Design and Development

I cannot get tax_query in get_posts() to work with custom taxonomy

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.

search-tags.php

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

function search_tags_closure() {
    $search_tags = new Search_Tags;

    $search_tags->create_taxonomy();
}

add_action('init', 'search_tags_closure');

search-tags.class.php

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';

        unset($post_types['attachment']);

        return array_keys($post_types);
    }

    protected function is_custom_post_type($name)
    {
        $built_in_post_types = [
            'post',
            'page',
        ];

        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.

functions.php

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

    return get_posts($args);
}

 var_dump(testing());