dongxichan8627 2018-08-31 10:14
浏览 60
已采纳

如何动态分配作者在WordPress中发布?

I'm creating a WordPress plugin which has a custom meta-box that lists checkboxes of authors and contributors. When checked, the list shows up on frontend at the end of the post. What I need to do is: When user clicks the name of a contributor, it reverts to archives page, but the post isn't listed under the name of that contributors.

How can I update and save a post under multiple contributors so that it is showed under the author's archives page?

This is the custom meta-box callback function and the function which is called when the post is saved:

function cd_meta_box_cb($post)
{
    global $post;
    echo'<b> Select the contributors that have contributed to this post: </b>';
    echo '<br><br>';
    wp_nonce_field('my_meta_box_nonce', 'meta_box_nonce');
    global $wpdb;

    $authors=$wpdb->get_results("SELECT wp_users.ID, wp_users.user_nicename 
    FROM wp_users INNER JOIN wp_usermeta 
    ON wp_users.ID = wp_usermeta.user_id 
    WHERE wp_usermeta.meta_key = 'wp_capabilities' 
    AND wp_usermeta.meta_value LIKE '%author%' OR wp_usermeta.meta_value LIKE '%editor%'  
    ORDER BY wp_users.user_nicename");

    $current_user = wp_get_current_user();
    foreach ($authors as $author) {
        $author_info=get_userdata($author->ID);
        //$author_role=$author_info->roles;
        $author_first_name=$author_info->first_name;
        $author_last_name=$author_info->last_name;
        if(strcmp($current_user->user_nicename,$author->user_nicename)==0)
        {       
            echo"<input type='checkbox' id='my_meta_box_check' name='my_meta_box_check[]'";
            echo"value=";
            the_author_meta('user_nicename', $author->ID);
            echo" checked disabled>";

            echo"<input type='hidden' id='my_meta_box_check' name='my_meta_box_check[]'";
            echo"value=";
            the_author_meta('user_nicename', $author->ID);
            echo">";
        }
        else
        {
            echo"<input type='checkbox' id='my_meta_box_check' name='my_meta_box_check[]'";
            echo"value=";
            the_author_meta('user_nicename', $author->ID);
            echo">";    
        }
        echo $author_first_name ." ". $author_last_name ." ";
        echo"(";
        echo"<label id='labelid' for='author'>";
        the_author_meta('user_nicename', $author->ID);
        echo"</label>";
        echo")";
        echo "<br />";
    }
}
//save custom data when our post is saved
function save_custom_data($post_id)
{
    global $post,$wpdb;

    $contributor=get_post_meta($post->ID, 'my_meta_box_check', true);
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'my_meta_box_nonce')) return;
    if (!current_user_can('edit_post')) return;
    if (isset($_POST['my_meta_box_check'])) 
    {
        update_post_meta($post_id, 'my_meta_box_check', $_POST['my_meta_box_check']);
        $tablename = $wpdb->prefix.'authorlist';
        $wpdb->insert($tablename,array('authorname'=>$post_id,'authorpost'=>$contributor));
    }
    else 
    {
        delete_post_meta($post_id, 'my_meta_box_check');
    }
}

add_action('save_post', 'save_custom_data');
  • 写回答

3条回答 默认 最新

  • dongzice4895 2018-08-31 14:10
    关注

    Add this to your theme's 'functions.php' or plugin.

    add_action( 'pre_get_posts', 'modify_author_query' );
    
    function modify_author_query( $query ) {
        // check if on front-end and author query is modified
        if ( ! is_admin() && is_main_query() && $query->is_author() ) {
            $author_name =  $query->query_vars['author_name'];
            //$userdata = get_user_by('slug',$author_name);
            //$userid = $user->ID;
    
            $meta_query = array(  
                array(
                    'key' => 'my_meta_box_check',
                    'value' => $author_name,
                    'compare' => 'LIKE'
                )
            );
            $query->set( 'meta_query', $meta_query );
    
            // unset the default author since we are using custom meta
            unset( $query->query_vars['author_name'] );
        }
    }
    

    Please note that above may show wrong results if part of a user name matches with another - try saving the data as comma separated string (should begin and end with comma) and replace 'value' => $author_name with 'value' => ','.$author_name.','.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!