Yes. If the tables are in the same database, you can access them with the wordpress database methods. See https://developer.wordpress.org/reference/classes/wpdb/
Thanks Steve,
Can anyone shed any light on how I might achieve the above? Specifically question 3?
Cheers
The easiest thing to do might be to create a shortcode, so you’d have something like
Welcome, [user]
Project: [project output="name"]
Status: [project output="status"]
Project address: [project output="client_address'].
Thanks, I’ll look into shortcode.
With my tables then, I’m guessing I will have to create a FK in projects, which links to the UserID (the default PK in WP)? I’m a bit stuck on how to do this. I know how to link the two in SQL, but I’m not sure how I link a query to the shortcode above!
Thanks again!
The shortcodes are just “links” to PHP code, so you’d use the WPDB methods to create your SQL statements and process the results.
Just curious: Why did you choose to use custom tables rather than custom post types?
Okay looks like I need to do some WPDB learning :/ I wonder if there are any plugins that can help.
Naivety? I don’t really understand what I’m doing with WP and I’m trying to make a custom client portal. I’ve looked into plugins but none of them seem to do what I want. What are custom post types?
Custom Post Types are like a post or page, but of a type you define and to which you can add custom fields, either via hand-coded PHP or plugins like Advanced Custom Fields.
Thanks Steve. That looks like quite a bit of coding know how.
I can’t believe it’s so hard to query single fields at a time and stick them on a page. Maybe I’m just being naive in thinking I can do it!
I think I’m getting closer to what I want. Using php snippets, I am able to insert the $wpdb->get_var command and pull single instances from the WP database, which works.
Maybe someone can help with the following questions.
1. On a page I have the following snippet
<?php
$username = $wpdb->get_var( "SELECT display_name FROM $wpdb->users WHERE ID=2" );
echo "{$username}";
This currently echos the display name from whoever user ID 2 is. How do I amend this statement so it shows the name of whoever’s logged in?
2. Going one step futher, I have created some custom tables i.e. wp_projects. For this one, I made this query.
<?php
$projectref = $wpdb->get_var( "SELECT pr_no FROM $wpdb->projects WHERE user_id=1" );
echo "{$projectref}";
However, it echos nothing, and comes up blank. Does the wpdb shortcode not work with custom tables?
3. If you can solve the above, can I go one step further with that code. You can see it’s just WHERE user_id=1, but I’d like it to display only the project number(s) relevant to the person logged in.
There is a FK in ‘projects’ user_id, which links to ‘wp_users’ ID.
Many thanks!
start with get_current_user() to get the current user’s user object.
Thank you, that worked. I changed it to
<?php
$current_user = wp_get_current_user();
$username = $wpdb->get_var( "SELECT display_name FROM $wpdb->users WHERE ID=$current_user->ID" );
echo "{$username}";
Please can you shed some light on questions 2 & 3 above?
use a print_r to see what’s in $projectref and correct the PHP — remove the {} around it.
I think this has gone as far as I’m willing to take it.
No problem Steve – not trying to offend here, I’m just trying to learn.
For anyone who’s interested, you don’t need the $wdbp prefix on custom tables in a query. Amending the code to below fixes it.
<?php
$current_user = wp_get_current_user();
$projectref = $wpdb->get_var(“SELECT pr_no FROM projects WHERE ID=$current_user->ID”);
echo “{$projectref}”;