我正在创建一个WordPress插件,该插件具有一个自定义元框,其中列出了作者和贡献者的复选框.选中后,该列表显示在帖子末尾的前端.我需要做的是:当用户单击贡献者的名称时,它会返回到“存档”页面,但帖子未在该贡献者的名称下列出.
如何在多个贡献者下更新和保存帖子,以使其显示在作者的存档页面下?
这是自定义meta-box回调函数,是保存帖子时调用的函数:
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.’,’.
标签:meta-boxes,wordpress,php 来源: https://codeday.me/bug/20191108/2009083.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。