在函数wordpress中不工作is_author(get_current_user_id()) [英] Not working is_author(get_current_user_id()) in functions wordpress

查看:155
本文介绍了在函数wordpress中不工作is_author(get_current_user_id())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题为已创建帖子的用户应用post_type,理论上我发现了wordpress的这些功能,但显然它不起作用

  function preview_publish_posts($ query){
if(is_user_logged_in()&& is_author(get_current_user_id())){
$ query-> set('post_status',数组('publish','pending'));
}
return $ query;
}

add_filter('pre_get_posts','preview_publish_posts');

或者函数可能无法从functions.php调用它。

解决方案

经过大量的游戏后,我发现不管我怎么尝试is_author(get_current_user_id())都不会返回true。我检查了这是否是get_current_user_ID的错误,但是这会返回正确的值。下面的代码为我解决了这个问题,但是我仍然想知道为什么is_author(get_current_user_id(())(get_current_user_id( )
$ b

 函数preview_publish_posts($ query){

$ curauth =(get_query_var('author_name'))? get_user_by('slug',get_query_var('author_name')):get_userdata(get_query_var('author'));如果(is_user_logged_in()&& $ curauth)($ is_admin()&& $ query-> is_main_query()&& is_author()){


) - > ID == get_current_user_id()){
$ query-> set('post_status',array('publish','pending'));
}
return;
}
}

add_action('pre_get_posts','preview_publish_posts');


I have problem to apply a post_type for users who have created their post, in theory I found those functions of wordpress, but apparently it does not work

function preview_publish_posts($query) { 
    if( is_user_logged_in() && is_author(get_current_user_id()) ) {
        $query->set('post_status', array('publish', 'pending'));
    }
    return $query;
}

add_filter('pre_get_posts', 'preview_publish_posts');

Or it is possible that the function is not valid for calling it from functions.php

解决方案

After a lot of playing around I found that no matter how I tried is_author(get_current_user_id()) would never return true. I checked to see if that was the fault of get_current_user_ID, but this would return the correct value. I ended up using a slightly different method to achieve the desired result.

The code below fixed the problem for me, however I would still like to know why is_author(get_current_user_id()) doesn't work in this case.

function preview_publish_posts($query) {

    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));

    if ( !is_admin() && $query->is_main_query() && is_author() ){
        if( is_user_logged_in() && $curauth->ID == get_current_user_id() ) {        
            $query->set( 'post_status', array('publish', 'pending'));
        }
        return;
    } 
}

add_action( 'pre_get_posts', 'preview_publish_posts' );

这篇关于在函数wordpress中不工作is_author(get_current_user_id())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆