• Hi there,

    I’m attempting to write a plugin, I’m using WP_Query and am trying to do a simple tax query with the following arguments:

    $args	=	array(
    	'posts_per_page'	=>	-1,
    	'post_type'		=>	'event',
    	'tax_query' 		=>	array(
    		array(
    			'taxonomy' => 'event_type',
    			'field'    => 'slug',
    			'terms'    => ‘carnival’,
    		),
    	),
    );

    This works fine in my theme and returns the relevant results, but in my plugin it doesn’t seem to return any results.

    Any help would be greatly appreciated.

    Many thanks,

    Chris

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    With themes, you can easily run custom queries by simply placing code on a template. The code runs when the template is loaded. Easy.

    With plugins, there are no templates. Plugins need to hook into something to insert content, or modify existing queries such that the main query does something other than the default.

    Without specific information on what you’re trying to do, that’s about all I can tell you.

    Thread Starter cboakes

    (@cboakes)

    Thanks for taking the time to reply bcworkz.

    I’m trying to write a plugin that returns JSON encoded results. A bit like JSON API but with the ability to query ACF fields and multiple taxonomy terms.

    I’ve constructed my query (basic example in the question) which, if I leave out the tax_query part works fine. However with the tax_query as an argument it doesn’t return anything.

    I wasn’t sure if WP_Query was the best way to approach this kind of query or whether I need to go down the $wpdb route.

    Moderator bcworkz

    (@bcworkz)

    Ah, I see 🙂 Taking a closer look, you have “curly quotes” around “carnival”. They must be the straight quote or apostrophe. FYI, if you checked your error logs you’d see entries for “syntax error, unexpected something at line something in some file” that’d have pretty much led you to this.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WP_Query tax_query in plugin’ is closed to new replies.