Skip Navigation

[Resolved] WP_Query does not work with taxonomy at the backend

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1163507

I have reused some code in order to get a CSV file export at the backend. Which works great until I add the taxonomy to the Query.
Here is the code: https://pastebin.com/rfyTFXaX

 $arg = array(
                'post_type' => 'certificate',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'list',
                        'field'    => 'slug',
                        'terms'    => $filter ,
                    )
                    ),
            );

I have also tried:

 $arg = array(
                'post_type' => 'certificate',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'list',
                        'field'    => 'slug',
                        'terms'    => array($filter) ,
                    )
                    ),
            );

But it only works like this:

 $arg = array(
                'post_type' => 'certificate',
                'post_status' => 'publish',
                'posts_per_page' => -1,
            );
#1163991

Your syntax for the tax_query looks okay, but I think the $filter variable might be assigned incorrectly. Try narrowing down the problem a bit:

add_action( 'init', 'func_export_all_posts' );
function func_export_all_posts() {
    if(isset($_GET['export_all_posts'])) {
        if(isset($_GET['list'])) {
            $filter = strval($_GET['list']);
        };
        $arg = array(
                'post_type' => 'certificate',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'list',
                        'field'    => 'slug',
                        'terms'    => array('slug-1', 'slug-2') ,
                    )
                    ),
            );

Replace slug-1 and slug-2 with two known taxonomy term slugs. Make sure that there is at least one published certificate post with both of these terms. Test again. If the problem is resolved, you know the issue is with the $filter variable assignment logic.

#1164556

Hi Christian
Thank you for your respons.
I have tried your suggestion but it did not help

$terms = get_terms([
    'taxonomy' => 'list',
    'hide_empty' => false,
]);
print_r($terms);

But It gives me this

WP_Error Object
(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid taxonomy.
                )

        )

    [error_data] => Array
        (
        )

)

And then I found this!
https://toolset.com/forums/topic/invalid-taxonomy-error-outside-theme-folder/
I seems to have done the trick. Is there an other way to call the first action without the need to remove/add it?

#1164850

Maybe change the priority setting on your init hook:

add_action( 'init', 'func_export_all_posts', 99 );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.