In WordPress I need to fetch name of author who created post using author_id.
How can I find author_name ?
You can use get_the_author_meta(), to get author data.
echo get_the_author_meta('display_name', $author_id);
Hope this helps!
This should work like charm
<?php echo get_the_author(); ?>
For more detailed information.
https://codex.wordpress.org/Function_Reference/get_the_author
Use below code in single.php or the relevant page you want author name
<?php get_the_author_meta( 'display_name', $author_id ); ?>
When used in the WordPress Custom REST API endpoint you can do it like this:
function customrestapiplugin_getpost( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data[$i]['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['excerpt'] = $post[0]->post_excerpt;
$data['slug'] = $post[0]->post_name;
$data['date'] = $post[0]->post_date;
$data['link'] = get_permalink($post[0]->ID);
$data['author'] = get_the_author_meta('display_name', $post[0]->post_author);
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
Add This code in single-post.php
<?php echo get_the_author(); ?>
I hope this will work !!
For those who are looking for a seamless solution on how to get an author of a post in WordPress, this is another lightweight method.
global $wpdb;
$post_id = 12; // your post id
$post_author_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_id ) );
$author = new WP_User( $post_author_id );
$display_name = $author->display_name;
$avartar = get_avatar( $post_author_id, 30 ); // get_avatar( userid, size )
$author_url = get_author_posts_url( $post_author_id );
Related
I have a custom post called 'project'.
I need to get 'titles' that logged in user wrote.
I tried the following code but it doesn't show any titles.
I'm a beginner... I looked into but I can't find which one has problems.
Would you please correct my code?
function output_projects_list() {
global $wpdb;
$custom_post_type = 'project'; // define your custom post type slug here
$current_user = get_userdata(get_current_user_id());
$current_user_name = $current_user->display_name;
// A sql query to return all the logged in users' post titles
$results = $wpdb->get_results( $wpdb->prepare( "
SELECT ID
, post_title
FROM {$wpdb->posts}
WHERE post_type = %s
, author = %s"
, and post_status = 'publish'
", $custom_post_type, $current_user_name ), ARRAY_A );
// Return null if we found no results
if ( ! $results )
return;
foreach( $results as $index => $post ) {
$output = $post['post_title'];
}
return $output;
}
echo output_projects_list();
Thank you.
I would use WP_Query instead, it's cleaner and easier to read. Take look at the following code:
function user_published_posts()
{
$query = new WP_Query(array(
"author" => get_current_user_id(),
"post_type" => "project",
"post_status" => "publish"
));
while ($query->have_posts()) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<span><?php the_author() ?></span>
<?php };
}
Let me know if it's what you're looking for!
I want to add custom endpoint in WordPress Rest API.I am able to fetch the post id,title,content,slug,categories and featured_image through this code by creating simple plugin. I am obtained category id in the Code.I want the category name i tried to do this by get_cat_name But did not understand it. How can i get category name,author name and author profile image through custom endpoint. I am also a beginner in wordpress. I refer to the documentation but did not understand how to do it. Write it as in w_posts function for category name
$data[$i]['catname']= get_cat_name($data[$i]['categories']);
<?php
/** Plugin Name : Custom API
* Plugin URI : https://google.com
* Decription : Crushing It
* Version : 1.0
* Author: Shahryar
* Author URI: https://google.com
*/
/** Plugin Name: Custom API... */
function w_posts(){
$args = [
'numberposts'=> 99999,
'post_type' => 'post'
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
$data[$i]['content'] = $post->post_content;
$data[$i]['slug'] = $post->post_name;
$data[$i]['categories'] = $post->post_category;
$data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
$i++;
}
return $data;
}
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function my_awesome_function( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function w_post( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['slug'] = $post[0]->post_name;
$data['categories'] = $post[0]->post_category;
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
add_action('rest_api_init',function(){
register_rest_route('w/v1','posts',[
'methods'=> 'GET',
'callback'=>'w_posts',
]);
register_rest_route( 'w/v1', 'posts/(?P<slug>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'w_post',
) );
});
You are getting category id then you can get category name using it' id like:
get_the_category_by_ID( $cat_ID )
To get the author name and image you can do as following:
$author_id=$post->post_author; //get author id
//get author image URL by author id
$author_img_url = the_author_meta( 'avatar' , $author_id );
//get author name by author id
the_author_meta( 'user_nicename' , $author_id );
I'm trying to get the featured image from the postid passed through the url.
http://www.example.com/schedule-appointment/?postid=589
I've managed to get the postid from the url, but everything goes down hill from there. I must be missing something simple. I'm not a programmer...would love some help.
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$Path=$_SERVER['REQUEST_URI'];
$control = array();
$control = explode('?', $Path);
$get = $control[1];
$get = explode('=', $get);
$get2 = $get[1];
$args = array(
'post_type' => 'page',
'post__in' => $get2,
);
// Fire up the Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ): $the_query->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->$get2) );
echo '$feat_image';
};
Try this
<?php
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$ID = isset( $_GET["postid"] ) ? $_GET["postid"] : false;
if( $ID ){
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full' );
$url = $thumb['0'];
echo "<img src ='".$url."' alt = 'Image'>";
}
}
?>
There is no need for the WP_Query , You have one id and you can easily get this done by using following code,
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$postid = $_GET['postid'];
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($postid) );
echo '$feat_image';
};
I saw in here that I can get a post's content in WordPress using the post ID. Something like:
<?php $my_postid = 83;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;?>
I want the very same thing, BUT getting the post by its name.
You can do it using
$content_post = get_posts( array( 'name' => 'yourpostname' ) ); // i.e. hello-world
if( count($content_post) )
{
$content = $content_post[0]->post_content;
// do whatever you want
echo $content;
}
Update : Also you can add this function in your functions.php and can call it from anywhere
function get_post_by_name($post_name, $post_type = 'post', $output = OBJECT) {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $post_name, $post_type ));
if ( $post ) return get_post($post, $output);
return null;
}
// call the function "get_post_by_name"
$content_post = get_post_by_name('hello-world');
if($content_post)
{
$content = $content_post->post_content;
// do whatever you want
echo $content;
}
Update : To get a post by it's title you can use
// 'Hello World!' is post title here
$content_post = get_page_by_title( 'Hello World!', OBJECT, 'post' );
or you can use your $item->item_title variable
$content_post = get_page_by_title( $item->item_title, OBJECT, 'post' );
if($content_post)
{
$content = $content_post->post_content;
// do whatever you want
echo $content;
}
Or even the author id from the post id. I am trying to return the author meta (author page link and avatar) in the sidebar of a single post page (outside of the post loop). What is the best way to do this? I am using a custom function (see below) to return the post id, but am not sure what function to call next.
function this_post_id() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
I figured it out.
<?php $author_id=$post->post_author; ?>
<img src="<?php the_author_meta( 'avatar' , $author_id ); ?> " width="140" height="140" class="avatar" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
<?php the_author_meta( 'user_nicename' , $author_id ); ?>
If you want it outside of loop then use the below code.
<?php
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'display_name' , $author_id );
echo $display_name;
?>
**use this code for display Auth Name**
<?php
$auth_id = $post->post_author;
echo get_the_author_meta( 'display_name', $auth_id );
?>
<?php
$field = 'display_name';
the_author_meta($field);
?>
Valid values for the $field parameter include:
admin_color
aim
comment_shortcuts
description
display_name
first_name
ID
jabber
last_name
nickname
plugins_last_view
plugins_per_page
rich_editing
syntax_highlighting
user_activation_key
user_description
user_email
user_firstname
user_lastname
user_level
user_login
user_nicename
user_pass
user_registered
user_status
user_url
yim
This should work
Post ID
$post_id = get_the_ID();
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'nickname' , $author_id );
echo $display_name;
Note: You need to use this function inside the loop.