最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - build child and anchestor three from post parent

programmeradmin4浏览0评论

I'm trying to build a three menu from a custom post type, and wanna build a complet three from the posts children and parents, with all levels both up and down included.

And actually the function wp_list_pages()does that really great, and actually includes all levels that i want.

But i also want to modify the query a little, and exclude some post that fits within a meta query, that i'm using another place like this:

$meta_query[] = array(
    'relation' => 'or',
    array(                
        'key' => 'rank',
        'compare' => '<=',
        'type' => 'NUMERIC',
        'value' => 100,
    ),            
    array(                
        'key' => 'rank',
        'compare' => 'NOT EXISTS',
        'value' => 1,
    ),
); 

The only problem is that wp_list_pages() don't support the meta_query, and only looking for at specific meta key and value.

Does anybody have a solution for this, or an idea to archive the goal?

I'm trying to build a three menu from a custom post type, and wanna build a complet three from the posts children and parents, with all levels both up and down included.

And actually the function wp_list_pages()does that really great, and actually includes all levels that i want.

But i also want to modify the query a little, and exclude some post that fits within a meta query, that i'm using another place like this:

$meta_query[] = array(
    'relation' => 'or',
    array(                
        'key' => 'rank',
        'compare' => '<=',
        'type' => 'NUMERIC',
        'value' => 100,
    ),            
    array(                
        'key' => 'rank',
        'compare' => 'NOT EXISTS',
        'value' => 1,
    ),
); 

The only problem is that wp_list_pages() don't support the meta_query, and only looking for at specific meta key and value.

Does anybody have a solution for this, or an idea to archive the goal?

Share Improve this question asked Nov 6, 2020 at 10:37 bymembymem 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

So i got it working, by adding a filter:

i added this before the wp_list_pages

add_filter( 'get_pages', 'check_propper_rank', 10, 2 );
wp_list_pages( array(
   'post_type' => 'wiki-term',
) );

and then created a new filter, to removed the ones that did'nt fit the rank.

function check_propper_rank( $pages, $arguments ) {
    
    $user_rank = 100;
    
    foreach ( $pages as $key => $page ) {
        $required_rank = get_post_meta($page->ID, 'rank', true);
        if ( $required_rank >= $user_rank ) {
            unset($pages[$key]);
        }
    }
    
    if ( empty( $pages ) )
        return $pages;

    // Remove instantly
    remove_filter( current_filter(), __FUNCTION__, 10 );

    return $pages;
}

发布评论

评论列表(0)

  1. 暂无评论