If you want to get Author ID outside the loop in WordPress? Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | $args= array( 'post_type' =>'any', 'post_status' => 'publish', 'order' => 'ASC', 'posts_per_page' => '-1' ); $posts = new WP_Query($args); $posts = $posts->posts; foreach($posts as $post) { switch ($post->post_type) { case 'page': // get the author's id through the post or page $id = get_post_field( 'post_author', $post->ID); // the first parameter is the name of the author // of the post or page and the second parameter // is the id with which the function obtains the name of the author. echo get_the_author_meta('display_name', $id); break; case 'post': $id = get_post_field( 'post_author', $post->ID; echo get_the_author_meta('display_name', $id); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.