Get the author ID - Wordpress, WP Job Manager - php

I am using Wp Job Manager plugin with job_listing as the post_type
On single job listing page, I am trying to show the list of all job listing posted by the same user > author.
I can't figure out how to correctly get the ID of the author of the post
I've been trying with 'author' => get_current_user_id() query, but that's not it.
This only fetches the ID of the current user, which is me as I'm logged in, and shows all my listings.
How can I get the the author of the post that I'm currently viewing?
This is what I'm currently trying, but it will only show my own listings as a logged in user:
function my_query_args($query_args, $grid_name) {
if ($grid_name == 'author-listings') {
global $current_user;
get_currentuserinfo();
// all query parameters can be modified (https://codex.wordpress.org/Class_Reference/WP_Query)
$query_args['author'] = $current_user->ID;
}
return $query_args;
}
add_filter('tg_wp_query_args', 'my_query_args', 10, 2);

global $post;
$author_id = $post->post_author;
If $post is undefined, are you really on a single page or is the setup you have working its' own magic?

To get the author ID, try
https://developer.wordpress.org/reference/functions/get_the_author_meta/
To simply display it, try https://developer.wordpress.org/reference/functions/the_author_meta/
So you could try
$query_args['author'] = $get_the_author_meta('ID');

Related

WordPress - Only Display Posts That Are Associated To Logged In User

In my WordPress Site, I have custom post types setup for Tanks, Locations, and Users using the PODS plugin. Currently I have some PHP that allows me to only show the logged in user a post, if they are listed as the author of it. Which is a good start, but not exactly how I need it to work. Because of the way WordPress stores their Author field, I am only able to have one User associated per Tank/Location instead of many. My question is, how would I go about making it so that WordPress will check to see what Tanks/Locations the current logged in user has associated to them, and only display those? In both the Tanks and Locations PODS, I have already setup a relationship field to my Users POD which allows me to assign each Tank/Location with as many users as necessary. All I need now is to figure out how display that information. If it makes any difference, the storage type for all my PODS is Table Based.
So in case anyone has the same question I did, here is how I ended up solving my issue. The variable 'users.ID' is referring to the relationship field I had in each of my Locations/Tanks.
if(!current_user_can('administrator')){
add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );
function pods_by_current_user_cpt( $atts, $content = null ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$atts['where'] = 'users.ID = ' . (int) $user_id;
return pods_shortcode( $atts, $content );
}
}
else{
add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );
function pods_by_current_user_cpt( $atts, $content = null ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
return pods_shortcode( $atts, $content );
}
}

Wordpress query users and display as taxonomy

I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.

How to display an acf field from a user profile?

I currently have an advanced custom field setup in the user's profile with a field name of author_title. This would display what the user does at the company blog.
I currently have the following working but the title only updates for first user and all users get that title instead of being able to use their own.
Updated
<?php $current_user = wp_get_current_user(); ?>
<h3><?php the_field('author_title', 'user_' . $current_user->ID); ?></h3>
Try to get user ID in another way.
Now you get ID for currently logged in user so it shows the same field everywhere. I assume you need show that filed for author posts so:
$author_id = get_the_author_meta('ID');
$author_field = get_field('author_title', 'user_'. $author_id );

trying to use query_posts('category_name=page_slug');

I´m trying to query posts after category name and want the ('category_name=cat-name'); cat-name to be taken from the page title, lets say the page title is "Forename Lastname" then the perma-link will be /forename-lastname/ . Is it possible to make the cat-name copy the "forname-lastname" with php or must I try to make some a javascript to take care of it?
The "permalink" of the page it's stored as its post_name, so you can get it from the global $post
For instance:
global $post;
$slug = $post->post_name;
$posts_in_cat = query_posts('category_name='. $slug );

Show posts in category that match username

This is fixed thanks to Alex's answer below.
So this is a bit of a weird one. I am setting up so I have category's with names "cat1, cat2, cat3" etc.
And say I had users called "cat1, cat2, cat3" so they matched the same as the category names.
I am then wanting to somehow only show the posts to the user that relates to their category, so basically if USER: "cat1" is logged in then they could only see the posts in category "cat1" as it matches their username.
I know you can do this to show posts only that the current user logged in has made but this wont work as the way the posts get put into the category complicated to explain.
<?php
// The Query
$args = array(
'post_status'=> '.....'
);
global $user_ID;
get_currentuserinfo();
if($user_ID) {
query_posts( $args,"author=$user_ID" );
}
?>
So if anyone has any insight into only showing posts to a user that matches a category with the same name as the user logged in hopefully they will be kind enough to help out.
It seems like you've started a round-about way of getting this accomplished. Since you have categories already set up with the username, you should just be able to place that in your query
//Get User -> ID -> Cat
global $current_user;
get_currentuserinfo();
$args = array(
category_name => $current_user->ID
); //or use $current_user->display_name if that's how they are set up.
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
//Display your loop how you want
endwhile;
// Reset Post Data
wp_reset_postdata();
EDIT:
did you do var_dump($current_user->ID) to see if it matches the category name? That variable from the object might only contain an ID (like "1" or "178" etc). If that was the case, you'd need something like
$the_cat_name = 'category' . $current_user->ID;
//Append your 'naming structure' to the front of the user ID
Then just replace the array with
$args = array(
category_name => $current_user->ID
);
Alternatively, look at the Current User Functions (here). You may be able to use
$current_user->user_login
instead. You could also just try var_dump($current_user) (after calling global $current_user; get_currentuserinfo();) and see what variable contains the string you need

Categories