admin管理员组文章数量:1330640
I'm working on a site that has a fairly large page structure a few levels deep - in some sections there are a lot of pages.
So I want to have functionality if user choose on select box level 1 then only pages in level 1 will show in below list of pages. if he choose level 2 then only level 2 pages, same go to level 3 level 4 level 5 level 6.
it's working for top level parent pages when I set query_vars['post_parent'] = 0; and I want to have same functionality to show list of level 1 child page,level 2 child pages and so on...
I am stuck on it. please I will be great full if anyone can help me for it. Thanks see screenshot link .png
function level_page_admin_posts_filter( $query ) {
global $pagenow;
if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['lavel_pages'])) {
$level = $_GET['lavel_pages'];
if($level == 'parent'){
$query->query_vars['post_parent'] = 0;
}else
if($level == 1){
}else
if($level == 2){
}
}
}
add_filter( 'parse_query', 'level_page_admin_posts_filter' );
function admin_page_filter_level_pages() {
global $wpdb;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
$sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'page' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
$parent_pages = $wpdb->get_results($sql, OBJECT_K);
$select = '
<select name="lavel_pages">
<option value="parent">Parent Pages</option>
<option value="1">Level 1</option>
<option value="2">Level 2</option>
<option value="3">Level 3</option>
<option value="4">Level 4</option>
';
$select .= ' </select>';
echo $select;
} else {
return;
}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_level_pages' );
I'm working on a site that has a fairly large page structure a few levels deep - in some sections there are a lot of pages.
So I want to have functionality if user choose on select box level 1 then only pages in level 1 will show in below list of pages. if he choose level 2 then only level 2 pages, same go to level 3 level 4 level 5 level 6.
it's working for top level parent pages when I set query_vars['post_parent'] = 0; and I want to have same functionality to show list of level 1 child page,level 2 child pages and so on...
I am stuck on it. please I will be great full if anyone can help me for it. Thanks see screenshot link https://i.sstatic/EKpy6.png
function level_page_admin_posts_filter( $query ) {
global $pagenow;
if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['lavel_pages'])) {
$level = $_GET['lavel_pages'];
if($level == 'parent'){
$query->query_vars['post_parent'] = 0;
}else
if($level == 1){
}else
if($level == 2){
}
}
}
add_filter( 'parse_query', 'level_page_admin_posts_filter' );
function admin_page_filter_level_pages() {
global $wpdb;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
$sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'page' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
$parent_pages = $wpdb->get_results($sql, OBJECT_K);
$select = '
<select name="lavel_pages">
<option value="parent">Parent Pages</option>
<option value="1">Level 1</option>
<option value="2">Level 2</option>
<option value="3">Level 3</option>
<option value="4">Level 4</option>
';
$select .= ' </select>';
echo $select;
} else {
return;
}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_level_pages' );
Share
Improve this question
edited Jul 25, 2020 at 2:11
Hafiz Shehroz
asked Jul 24, 2020 at 21:57
Hafiz ShehrozHafiz Shehroz
113 bronze badges
1 Answer
Reset to default 0So it looks like get_ancestors()
is the function you need to be able to figure out what 'level' something is. The number of results returned from get_ancestors corresponds to your 'level' number, with 0 results meaning parent page.
And so the problem you have is that this number isn't available as a query argument. Therefore, perhaps the simplest option would be to store this as a meta field against all your pages when they're saved. You could achieve this with a simple hook, like:
add_action('save_post', 'hafiz_update_level_number', 10, 2);
function hafiz_update_level_number($postID, $post) {
if ($post->type == 'page') {
update_post_meta($postID, 'hafiz_level_number', get_ancestors($postID)) ;
}
}
Now don't need to have the if ($level == ?)
code, you just make sure you have a number where 0 is 'parent' and 1 is level 1, 2 is level 2, etc. and you can make your query work something like this:
$query->query_vars['meta_key'] = 'hafiz_level_number';
$query->query_vars['meta_value'] = $levelNumber;
You may have to edit this for how you're using this $query object, but hopefully you get the general idea here.
Note you have the obvious problem that this new value is not set unless a post has been saved or updated after you install this hook, so you will need to either manually open and save every page, or write a quick script to run once which loops through every page and sets this value for every page.
Hope that's helpful. This code is untested but the intention is to outline a way you can write the complete code you need to answer your question. Please respond with any questions
本文标签: phpfilter default query to show just selected level of child pages in wordpress
版权声明:本文标题:php - filter default query to show just selected level of child pages in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742233024a2437570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论