admin管理员组文章数量:1334404
function fws_admin_posts_filter( $query ) {
global $pagenow;
if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['my_parent_pages'])) {
$query->query_vars['post_parent'] = $_GET['my_parent_pages'];
}
}
add_filter( 'parse_query', 'fws_admin_posts_filter' );
function admin_page_filter_parentpages() {
global $wpdb;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'books') {
$sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'books' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
$parent_pages = $wpdb->get_results($sql, OBJECT_K);
$select = '
<select name="my_parent_pages">
<option value="">Parent Pages</option>';
$current = isset($_GET['my_parent_pages']) ? $_GET['my_parent_pages'] : '';
foreach ($parent_pages as $page) {
$select .= sprintf('
<option value="%s"%s>%s</option>', $page->ID, $page->ID == $current ? ' selected="selected"' : '', $page->post_title);
}
$select .= '
</select>';
echo $select;
} else {
return;
}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_parentpages' );
This code gives me the expended filter dropdown option but once I select one of the option and hit filter then the return page is empty.
Once I select one of them and hit filter, the result page can't bring back the result.
function fws_admin_posts_filter( $query ) {
global $pagenow;
if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['my_parent_pages'])) {
$query->query_vars['post_parent'] = $_GET['my_parent_pages'];
}
}
add_filter( 'parse_query', 'fws_admin_posts_filter' );
function admin_page_filter_parentpages() {
global $wpdb;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'books') {
$sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'books' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
$parent_pages = $wpdb->get_results($sql, OBJECT_K);
$select = '
<select name="my_parent_pages">
<option value="">Parent Pages</option>';
$current = isset($_GET['my_parent_pages']) ? $_GET['my_parent_pages'] : '';
foreach ($parent_pages as $page) {
$select .= sprintf('
<option value="%s"%s>%s</option>', $page->ID, $page->ID == $current ? ' selected="selected"' : '', $page->post_title);
}
$select .= '
</select>';
echo $select;
} else {
return;
}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_parentpages' );
This code gives me the expended filter dropdown option but once I select one of the option and hit filter then the return page is empty.
Once I select one of them and hit filter, the result page can't bring back the result.
Share Improve this question edited Jun 16, 2020 at 18:55 Moumita akter asked Jun 16, 2020 at 18:19 Moumita akterMoumita akter 52 bronze badges 2- 1 Welcome to WPSE. Can you edit your post so that there is a question to answer? – jdm2112 Commented Jun 16, 2020 at 18:23
- The filtered page gives me an empty page. I want the filter result – Moumita akter Commented Jun 16, 2020 at 18:33
1 Answer
Reset to default 0Let's if it helps you. Instead of adding a custom query var if we can use directly from url that might solve the issu. First make post_parent
loadable from url.
function make_post_parent_public_qv() {
global $pagenow;
if ( is_admin() && $pagenow == 'edit.php' )
$GLOBALS['wp']->add_query_var( 'post_parent' );
}
add_action( 'init', 'make_post_parent_public_qv' );
Now focus on the filter:
function admin_page_filter_parentpages() {
global $wpdb;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'books') {
$sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'books' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
$parent_pages = $wpdb->get_results($sql, OBJECT_K);
$select = '
<select name="post_parent">
<option value="">Parent Pages</option>';
$current = isset($_GET['post_parent']) ? $_GET['post_parent'] : '';
foreach ($parent_pages as $page) {
$select .= sprintf('<option value="%s"%s>%s</option>', $page->ID, $page->ID == $current ? ' selected="selected"' : '', $page->post_title);
}
$select .= '
</select>';
echo $select;
} else {
return;
}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_parentpages' );
Give it a try and let me know.
本文标签: Filter in Custom post type to find the parent post
版权声明:本文标题:Filter in Custom post type to find the parent post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742343150a2457003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论