簡體   English   中英

如何動態分配作者在WordPress中發布?

[英]How to dynamically assign author to post in WordPress?

我正在創建一個WordPress插件,它有一個自定義元框,列出了作者和貢獻者的復選框。 選中后,列表會顯示在帖子末尾的前端。 我需要做的是:當用戶單擊貢獻者的名稱時,它將恢復到存檔頁面,但該帖子未列在該貢獻者的名稱下。

如何在多個貢獻者下更新和保存帖子,以便在作者的檔案頁面下顯示?

這是自定義元框回調函數和保存帖子時調用的函數:

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');

將其添加到主題的'functions.php'或插件中。

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'] );
    }
}

請注意,如果用戶名的一部分與另一個匹配,上面可能會顯示錯誤的結果 - 嘗試將數據保存為逗號分隔的字符串 (應以逗號開頭和結尾)並將'value' => $author_name'value' => ','.$author_name.','

好吧,首先,Wordpress打算在帖子中有一位作者。 所以我們需要從那里開始。

我的方法是使用Wordpress作者字段作為主要作者,並為“次要”作者擁有自己的字段。

通過這種方式,您可以直接連接到作者頁面查詢,並添加除了用戶是作者的帖子之外,還可以在meta下保存用戶的保存位置。

沒有引用任何代碼,因為你的問題似乎比不知道代碼更錯誤。

add_filter('posts_where', function($where){
      if(!is_admin() && is_author() && is_main_query() ){
            $where = "AND ((dev_posts.post_author = ".get_queried_object_id().") OR ( ( dev_postmeta.meta_key = 'auth_id' AND dev_postmeta.meta_value LIKE '%:\"'.get_queried_object_id().'\"\;%' ) )) AND dev_posts.post_type = 'post' AND (dev_posts.post_status = 'publish' OR dev_posts.post_status = 'private')";
    }
    return $where;
});

add_action('pre_get_posts', function($query){
    if(!is_admin() && is_author() && is_main_query() ){
        $query->set('relation', 'or');
        $query->set('meta_query', array(array(
            'key' => 'auth_id',
            'value' => ':'.get_queried_object_id().';',
            'compare' => 'LIKE'
        )));
    }
}); 

“dev”是我的數據庫前綴,因此您需要使用$ wpdb-> prefix替換

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM