• Hi guys, I am trying to display specific Authors for individual Posts but my <?php the_author(); ?> is only displaying the Administrator’s display name. I’ve enabled a dropdown for “Author” within the Posts edit page, which contains a list of all Users (they are all “Editors” other than the one “Administrator”), but even when I select an Author from the dropdown the site only displays the Administrator name.

    Furthermore when viewing All Posts, the correct Author that I selected from the dropdown IS displaying in the Author column next to the corresponding post, but then the site itself still only displays the Administrator.

    Within the Blog Template PHP I’ve tried <?php the_author(); ?>, <?php the_modified_author(); ?> and <?php the_author_meta(‘display_name’, 1); ?>, but to no avail.

    You can check out the blog here: http://bottomlineblack.com/blog/

    Any help is appreciated, thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    First let’s determine for sure which author is actually stored in the DB. Because almost any output in WP can be filtered and altered, we should go to phpMyAdmin to investigate. The posts table lists the author ID for every post. The original administrator’s ID from when WP was installed should be 1. Double check by looking at the users table.

    If all the author IDs are for the administrator user, the selected author you’ve added is not saved as the true author. If it were saved in postmeta instead, you cannot use the_author() to display this value, it would need to come from get_post_meta().

    If the stored author IDs are all correct, a plugin or your theme is somehow altering the output of the_author(). Narrow down the source by deactivating all plugins and switching to a default twenty* theme. The correct author should now be listed. Begin restoring your setup, one module at a time. When the author output becomes incorrect again, the last activated module is the culprit.

    BTW, the_author_meta(‘display_name’, 1); will always return the administrator’s name because you are passing the user ID 1 to the function 🙂 The ID needs to come from the post object. “author” in this case is a misnomer, it really ought to be the_user_meta().

    Thread Starter bestewar

    (@bestewar)

    @bcworkz, thanks so much for the info! After investigating via phpMyAdmin, it would appear as though all of the post have 1 as the post_author, as you can see here: post author screenshot

    Any idea how I can update the authors? I’ve looked into the_user_meta() but haven’t found an exact solution. Thanks again!!

    • This reply was modified 6 years, 8 months ago by bestewar.
    Moderator bcworkz

    (@bcworkz)

    Saving the “author” in post meta would be a reasonable option, but you’d need to use custom code to display this value. “the_author” filter can be used for this. To save the selected drop down value to post meta, use the “save_post” action. Get the drop down selection from the $_POST array (keyed by field name) and save it with update_post_meta().

    Be sure to coordinate the data type passed in $_POST with the one used in “the_author”. If they are all the username, then you’re good. If the user ID is somehow involved instead, you’ll need to get the user object with the ID to work out the name.

    Alternately, you could set the actual post_author field to the selected user’s ID in the “wp_insert_post_data” filter. I’m not too keen on this approach because you lose track of who actually created the post. I generally don’t like to destroy data even if it seems to not matter at the time. YMMV

    Thread Starter bestewar

    (@bestewar)

    @bcworkz thanks again for the follow up, but you lost me a bit here. Here are two screenshots of what I’m working with in regards to assigning the authors via the WP dashboard: post author dropdown, posts authors column

    When you say I need to get the dropdown selection the $_POST array, is this an array that is already declared? Or if not, is this something within functions.php, or just within the blog template PHP file? Also, not exactly sure how to coordinate the_author filter.

    I apologize for the naivety, but I think i’ll need a bit more hand holding to get this one wrapped up. Truly appreciate your help!

    Moderator bcworkz

    (@bcworkz)

    I’m happy to keep you pointed in the right direction, but I’m not able to completely line everything out for you either. If you can typically self-learn things with some direction, this can work. Otherwise you would be better off utilizing a resource than can devote their full attention. Such as hiring help through something like jobs.wordpress.net or jetpack.pro.

    While getting values out of $_POST and saving them is the normal approach and can solve your problem, I’d forgotten that the “pseudo” author values are showing up on the admin posts list screen. Thus the data is clearly saved somewhere and re-saving the same value for yourself elsewhere is redundant, inefficient, and just poor practice. Apologies for my lapse. You should make a concerted effort in determining how this is accomplished before working on saving the value yourself.

    How did this author metabox and the pseudo authors in the author column come to be? A plugin? Your theme? Where the data is stored can be determined by examining the existing code. Often easier said than done. Unless there is a custom table for such information, post meta is the logical place for this. It could conceivably be stored as an array of post IDs in user meta, but that would be very impractical. I think it’s in post meta, you’ve just failed to recognize it so far.

    If you were looking for author names, it could be the data was saved as user ID. It could also be stored in an array of other data, making searching a bit more difficult. I’d recommend making a concerted effort to find where the data is stored before re-saving it with your own methods. Utilize phpMyAdmin and as needed, the LIKE %…% operator while searching post meta. This will find data in serialized arrays as well as stand alone values. Try looking for the user ID as well as the various possible names associated with users. Using LIKE %…% with single digit IDs will likely yield a lot of false positives though. Use discretion.

    If you can determine where this data is stored, using “the_author” filter will be a lot easier. I’ll skip over instruction for saving values in $_POST for now since it’s not necessary if the data can be found. Read up on filter and action hooks in general in the Plugin Handbook. Also check out get_the_author() source code to see how “the_author” filter is actually applied and the return value used.

    This particular code uses the ternary operators, which is confusing if you’re not familiar with it. Here it is used as a safety valve to prevent code from throwing warnings in the off chance $authordata is not an object. In general $authordata will always be an object, so the statement without the ternary logic would simply be:
    return apply_filters('the_author', $authordata->display_name );

    When you hook this filter, your callback function is passed $authordata->display_name, which in your case, will always be that for admin user ID 1. Whatever is returned by your callback is then returned by the apply_filters() call, which in this case is returned by the get_the_author() call. In many cases the return is assigned to a variable instead of immediately being returned.

    What’s with get_the_author() when you are trying to use the_author()? It’s called by the_author(). As is often the case, check out the source code. The logic here is a little confusing because of the deprecated parameter. The default case is what is returned from get_the_author() is immediately echoed out. It’s possible to cause the value to be returned instead, but then we should simply call get_the_author() directly if that is what we want.

    It should now be clear the goal of your callback is to return the display name of the user stored as the pseudo author of the current post. But what’s the current post? An important tenet of PHP is all variables within a function are local to the function. Outside variables are not available within the function and variables within the function are not available outside. One of the few exceptions are global values. Since the_author() is always used in the Loop, the current post is stored in the global $post. Anytime you wan to use a global, you must declare it within the scope of its use. Thus your callback must contain this statement: global $post;

    With the current post, get the stored pseudo author for it and return their display name. You’ll likely need to do some intermediate steps, like getting the ID property within the $post object and maybe getting the display name based on the user ID. You should be able to find out how to do such things by searching. If you have trouble with anything, let me know. Good luck!

    Thread Starter bestewar

    (@bestewar)

    @bcworkz thanks again for the response! I do have quite a bit of experiencing with PHP and designing themes from scratch and do consider myself a quick learner so I am assuming this is something I’ll be able to figure out on my own. Thanks for the resources otherwise though.

    After digging a bit deeper in PhpMyAdmin it appears as though each post revision is being saved as it’s own entry in my wp_posts table, as you can see here: post revisions in PhpMyAdmin.

    With that said, it appears though the correct post_author id IS showing up for the “published” post, which is where I’m assuming the admin posts list screen is getting this data from. On that note, I’m at a loss as to why the blog itself is then echoing the admin only. I’ve tried <?php the_modified_author(); ?> but to no avail. I know we’re close, but what am i doing wrong?

    Thread Starter bestewar

    (@bestewar)

    @bcworkz, I’ve figured it out out a workaround. If i actually log in and as the user i want to have assigned as the author, go to the Post, click Update (even if I didn’t change anything), then the correct modified author is echoed on the blog page.

    This doesn’t affect the Author dropdown though. Just because I’m curious, do you have a solution or suggestion as to how I could echo the dropdown selection? Thanks again for all your help

    Moderator bcworkz

    (@bcworkz)

    If the correct author is in the published post record of posts table, get_the_modified_author() would be the wrong function, because that value is in post meta and reflects the last true modifier, not the selected author. At least it gives you a workaround.

    To display the author name related to the ID in published posts, the_author(); or echo get_the_author(); should display that author’s display name. As you’ve verified the desired author ID is indeed stored with the published record, the only reasonable explanation for the_author() not working is filter interference by plugin or theme. As I mentioned in my first reply, reverting to the default state will cause the desired author to be output. Isolating the module filtering the result is a matter of restoring your configuration one by one and continually testing.

    The only way for the wrong author to display in the default state is if core code had been modified. No one should be altering core code. If that has happened, perform a manual WP update, except use the version you currently are using. This replaces all core files with the correct, unaltered versions.

    Thread Starter bestewar

    (@bestewar)

    Thanks again @bcworkz, you are the man!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Only Displaying Administrator’ is closed to new replies.