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,
);
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.
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?
Maybe change the priority setting on your init hook:
add_action( 'init', 'func_export_all_posts', 99 );