Question

I am trying to work with custom taxonomies and I created separate php file which handles my ajax requests. PHP file is supposed to work with custom taxonomies but when I try

require_once( "../../../../wp-load.php" );

$taxonomyName = 'location';
$termz = get_terms($taxonomyName);

I get WP_Error which states Invalid taxonomy. The taxonomy exists 100% and the same code works perfectly fine from index.php. What is causing this? Also I tried "category" as taxonomy name and it worked so I am not sure why this happens. Can anyone offer help please? Thanks

Was it helpful?

Solution

try below code

          $taxonomies = array( 'location' );
                $args = array(
                    'hide_empty'    => false,
                    'fields'        => 'all', 
                    'hierarchical'  => true
        ); 
        $termz = get_terms( $taxonomies, $args );

if your file is in theme directory change path to

$base_dir = dirname(dirname(dirname(dirname(__FILE__))));
require_once($base_dir."/wp-load.php");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top