admin管理员组文章数量:1130349
I a problem with getting the query for custom taxonomy to work in this function. The $filter is filled with data but the get_posts() does not use it? The query works without the tax_query and there are: custom posts with the correct taxonomy(list)... want am I missing?
add_action( 'restrict_manage_posts', 'add_export_button' );
function add_export_button() {
$screen = get_current_screen();
if (isset($screen->parent_file) && ('edit.php?post_type=certificate'==$screen->parent_file)) {
?>
<input type="submit" name="export_all_posts" id="export_all_posts" class="button button-primary" value="Export All Posts">
<script type="text/javascript">
jQuery(function($) {
$('#export_all_posts').insertAfter('#post-query-submit');
});
</script>
<?php
}
}
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' => $filter ,
)
),
);
global $post;
$arr_post = get_posts($arg);
if ($arr_post) {
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Post Title', 'URL'));
foreach ($arr_post as $post) {
setup_postdata($post);
fputcsv($file, array(get_the_title(), get_the_permalink()));
}
exit();
}
}
}
I a problem with getting the query for custom taxonomy to work in this function. The $filter is filled with data but the get_posts() does not use it? The query works without the tax_query and there are: custom posts with the correct taxonomy(list)... want am I missing?
add_action( 'restrict_manage_posts', 'add_export_button' );
function add_export_button() {
$screen = get_current_screen();
if (isset($screen->parent_file) && ('edit.php?post_type=certificate'==$screen->parent_file)) {
?>
<input type="submit" name="export_all_posts" id="export_all_posts" class="button button-primary" value="Export All Posts">
<script type="text/javascript">
jQuery(function($) {
$('#export_all_posts').insertAfter('#post-query-submit');
});
</script>
<?php
}
}
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' => $filter ,
)
),
);
global $post;
$arr_post = get_posts($arg);
if ($arr_post) {
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Post Title', 'URL'));
foreach ($arr_post as $post) {
setup_postdata($post);
fputcsv($file, array(get_the_title(), get_the_permalink()));
}
exit();
}
}
}
Share
Improve this question
asked Dec 9, 2018 at 13:38
JesperJesper
116 bronze badges
4
|
2 Answers
Reset to default 0I'm fairly sure you just need to pass an array for terms like this:
$arg = array(
'post_type' => 'certificate',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'list',
'field' => 'slug',
'terms' => array($filter)
)
),
);
The problem was that I used the toolset plugin to generate the taxonomy which caused some priority confusion. Adding this to my functions.php solved the problem:
remove_action('init', 'func_export_all_posts');
add_action('init', 'func_export_all_posts', 99);
Ref: https://toolset/forums/topic/wp_query-does-not-work-with-taxonomy-at-the-backend
I a problem with getting the query for custom taxonomy to work in this function. The $filter is filled with data but the get_posts() does not use it? The query works without the tax_query and there are: custom posts with the correct taxonomy(list)... want am I missing?
add_action( 'restrict_manage_posts', 'add_export_button' );
function add_export_button() {
$screen = get_current_screen();
if (isset($screen->parent_file) && ('edit.php?post_type=certificate'==$screen->parent_file)) {
?>
<input type="submit" name="export_all_posts" id="export_all_posts" class="button button-primary" value="Export All Posts">
<script type="text/javascript">
jQuery(function($) {
$('#export_all_posts').insertAfter('#post-query-submit');
});
</script>
<?php
}
}
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' => $filter ,
)
),
);
global $post;
$arr_post = get_posts($arg);
if ($arr_post) {
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Post Title', 'URL'));
foreach ($arr_post as $post) {
setup_postdata($post);
fputcsv($file, array(get_the_title(), get_the_permalink()));
}
exit();
}
}
}
I a problem with getting the query for custom taxonomy to work in this function. The $filter is filled with data but the get_posts() does not use it? The query works without the tax_query and there are: custom posts with the correct taxonomy(list)... want am I missing?
add_action( 'restrict_manage_posts', 'add_export_button' );
function add_export_button() {
$screen = get_current_screen();
if (isset($screen->parent_file) && ('edit.php?post_type=certificate'==$screen->parent_file)) {
?>
<input type="submit" name="export_all_posts" id="export_all_posts" class="button button-primary" value="Export All Posts">
<script type="text/javascript">
jQuery(function($) {
$('#export_all_posts').insertAfter('#post-query-submit');
});
</script>
<?php
}
}
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' => $filter ,
)
),
);
global $post;
$arr_post = get_posts($arg);
if ($arr_post) {
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Post Title', 'URL'));
foreach ($arr_post as $post) {
setup_postdata($post);
fputcsv($file, array(get_the_title(), get_the_permalink()));
}
exit();
}
}
}
Share
Improve this question
asked Dec 9, 2018 at 13:38
JesperJesper
116 bronze badges
4
-
Not sure it would affect anything, but don't think you need that
global $postin there as you're querying the posts to the$arr_postarray – Tim Hallman Commented Dec 9, 2018 at 19:26 -
Other than that, perhaps try adding
'operator' => 'IN'to your tax_query array. – Tim Hallman Commented Dec 9, 2018 at 19:27 -
Actually, wrap your
'terms'in an array, like this'terms' => array($filters). I believe that is your problem. – Tim Hallman Commented Dec 9, 2018 at 19:30 - Thank you very much for your reply, Tim. I have now tried: 'operator' => 'IN' and to place the terms in an array and the drop the "global $post". But nothing seems to work when making the query for custom taxonomy. – Jesper Commented Dec 10, 2018 at 9:50
2 Answers
Reset to default 0I'm fairly sure you just need to pass an array for terms like this:
$arg = array(
'post_type' => 'certificate',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'list',
'field' => 'slug',
'terms' => array($filter)
)
),
);
The problem was that I used the toolset plugin to generate the taxonomy which caused some priority confusion. Adding this to my functions.php solved the problem:
remove_action('init', 'func_export_all_posts');
add_action('init', 'func_export_all_posts', 99);
Ref: https://toolset/forums/topic/wp_query-does-not-work-with-taxonomy-at-the-backend
本文标签: Query a custom taxonomy in a function to create an csv file
版权声明:本文标题:Query a custom taxonomy in a function to create an csv file 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749106921a2316725.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


global $postin there as you're querying the posts to the$arr_postarray – Tim Hallman Commented Dec 9, 2018 at 19:26'operator' => 'IN'to your tax_query array. – Tim Hallman Commented Dec 9, 2018 at 19:27'terms'in an array, like this'terms' => array($filters). I believe that is your problem. – Tim Hallman Commented Dec 9, 2018 at 19:30