WordPress的 - 隐藏在条件表行

问题描述 投票:1回答:1

我有一个自定义CPT称为主题只有一个编辑器或管理员可以创建一个。有自定义列和自定义元框。它仅针对后端目的而作出,而不是出版。所以,它有一个meta框,这将有作者的列表,然后选择一个将其分配给作者,并会在名称中显示将显示在具有自定义列名作者表。所以,我想作者只看到那些已经通过meta框分配给他们的主题。其它的主题应该被隐藏。有没有一种方法来隐藏与条件的特定行。 “cruiser_writer_value_key”将节省作者的名字。所以我的条件。

$writer_list_value = get_post_meta( $post_id, 'cruiser_writer_value_key', true );
$current_user = wp_get_current_user();

if($current_user->display_name == $writer_list_value){
    //display post row
}

else{
    //hide post row of the column
    }

我真的卡在这。和是的,我有自定义列了。这只是我不想给作者,编辑或读取尚未分配给他们的主题。所以,我的自定义CPT是这样和自定义列在那里。更多细节见图片。 Picture of the Topic CPT with custom Columns.

wordpress custom-post-type
1个回答
1
投票

使用filter_posts_list过滤柱ADD_ACTION( 'pre_get_posts', 'filter_posts_list');

function filter_posts_list($query)
{
    //$pagenow holds the name of the current page being viewed
     global $pagenow, $typenow;  

     $user = wp_get_current_user();
        $allowed_roles = array('author');
        //Shouldn't happen for the admin, but for any role with the edit_posts capability and only on the posts list page, that is edit.php
        if(array_intersect($allowed_roles, $user->roles ) && ('edit.php' == $pagenow) &&  $typenow == 'your_custom_post_type')
        { 
        //global $query's set() method for setting the author as the current user's id
        $query->set(
        'meta_query', array(
        array(
            'key'     => 'yout_meta_key',
            'value'   => $user->display_name,
            'compare' => '==',
        ),
        )
        ); // here you can set your custom meta field using meta_query.
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.