I have a custom post with 2 fields, the first field being the user, and the second one being a table, and whenever a user is logged in, I must display all of the posts that have the user in the 1st field, this is what I found:
In wp_user the ID of the user I'll be using to test it will have: user_id = 3
In wp_postmeta, I have a row with these values:
-post_id=92
-meta_value=3
And in wp_posts, in the ID = 92, I have the specific post name I was looking for.
I'm pretty confused on how to make this code dynamically, so any user will be able to see their own posts. And BTW, the post_author is the admin, so I can't use that field
Basiclly, I must retrieve the post_id from wp_postmeta by only using the the meta_value which I've seen a couple of guides but can't really understand how to do it. I know I must use wp_get_current_user(). Please someone explain me with detail how to achieve this.
Thanks
In your functions.php you need to place this hook , this hook is fired whenever a post is added or updated you need to use this for saving post id in the post meta table.
function enter_post_id( $post_id ) {
wp_get_current_user() // Get current user id with this
update_post_meta()($post_id, $meta_key, $meta_value, $unique)
// add a meta key say 'user_id' and place the value of user_id as meta value
}
add_action( 'save_post', 'enter_post_id' );
Now You have the match completed , you just need to query through the post meta table and get the post id for the user , and then from wp_posts show only the post's with matched ID.
Hope this helps . Feel free to ask if you have any confusion.
No problem :) Use this function.
function get_post_id(){
global $wpdb;
$result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key='user_id' AND meta_value= $user_id ", OBJECT );
// Where $user_id is the id you get from wp_current_user()
}
Now var dump this $result to see the post_id
loop through this to get values for each post id
Related
Wordpress postmeta table has rows with meta_value field_RANDOM_STRING. How do I find field info via database?
Sample:
field_5617ad2082500
field_56d6bddfc36f7
field_5617b3adf5c19
I want to extract info about this field directly via database without going through WordPress / Wordpress Code.
I guess this has something to do with custom field types but I am not sure.
I am migrating the system out of wordpress so wanted help in extracting data.
You can use such code
global $wpdb;
$fields = $wpdb->get_results("SELECT meta_key FROM $wpdb->postmeta WHERE meta_value = 'field_58756ba2297d4' LIMIT 1");
$meta_key = ltrim($fields[0]->meta_key,'_');
$meta_value = get_post_meta($post_id, $meta_key, true);
First you get row from database where you will get meta_key for this value, then you should trim if from _ in the begining of string, because its key for field id, and then you can get actual meta value using prepeared meta_key name;
Ok. Here's the problem.
I am creating 2 posts in here. First one is parent post and second one is a attachment post.I want to create a custom guid while inserting the parent post. e.g.
guid => http://localhost/music/?post_type=pdf&p=1548
I tried inserting the wp_insert_post first and then updating it with the update post function but it didn't work. Can you help me out in here because I am stuck and I am unable to sort,also another related question.
Can you get the current insert id while inserting the post?
Try to use raw sql:
global $wpdb;
$updateguid = $wpdb->get_results("UPDATE wp_posts SET guid ='what you want' WHERE ID ='inserted-id'");
I am creating a simple db for School Students and want to achieve it in wordpress using any method that is available now.
Every student details like first name, class etc will be saved in database along with ID as primary key.
In PHP, there used to be ONE file like details.php etc and based on QUery passed using GET or POST method, it will display the details. Can we do the same in Wordpress using a SINGLE Page or Post;
Instead of creating seperate Page / Post for every student, i had created PHP Queries in Page / Post using WP Plugin which will display the details of student based on querying ID.
But i am not sure how to make it generalized so that on entering http://mywpsite.com/studentpageorpost/?id=10 i should get the details of student with ID 10; and on entering http://mywpsite.com/studentpageorpost/?id=11, i should get the details of ID 11;
Can anyone please help on this.
If I understand well the code would work like this:
1 Take the id number from url and stored
1.1 htmlspecialchars() is for converting html tags so you can't be hacked by php injection
$id = htmlspecialchars( $_GET["id"] );
2 Here we have stored the user info in a object with the wordpress function get_userdata();
2.1 If ID not found return false.
$user_data = get_userdata( $id );
Accessing Usermeta Data
$user_data = get_userdata( $id );
echo $user_data->last_name . ", " . $user_info->first_name . "\n";
Results in:
Doe, John
If you want to know how to access more user info use print_r($user_data); and will output all the info that the user has.
Here are some of the useful values in the wp_users and wp_usermeta tables you can access with this function for use in your theme or plugin:
users
ID
user_login
user_pass
user_nicename
user_email
user_url
user_registered
display_name
user_meta
user_firstname
user_lastname
nickname
description
wp_capabilities (array)
admin_color (Theme of your admin page. Default is fresh.)
closedpostboxes_page
primary_blog
rich_editing
source_domain
Edit: I think the above system is the easiest still as MarkBlythe sed, no need for individual account, you can use custom post type plugin and custom fields. You could add the students very fast in a loop and array with this function wp_create_user( $username, $password, $email );
I want to know how many posts posted by a particular author. For example. There are 10 authors and each of them posts , now i want to know the count of posts done by the author.
Blog: viyugam.com
Post count is returned by this function for user whose id is passed to it . this can help
Use single user post
count_user_posts( $userid );
or
pass the user_id's in array to this function, to get the post count of users
count_many_users_posts($user_ids_array);
Reference: http://codex.wordpress.org/Function_Reference/count_user_posts
I have a table posts with columns post_id, user_id, post.
When a user visits his page, I retrieve a list of his posts (select post from posts where user_id=$uid). He can then add one or more posts to the table through a form.
Is there any way I can retrieve these newly-added posts without querying the entire table again?
I'm particularly interested in using this with jQuery to auto-refresh his posts periodically (like YouTube comments when 'auto-refresh' is selected, for example).
Thanks, Albert
If I were you, I would keep track of when the posts were made, e.g.
post_id, user_id, post, made
You can use AJAX to ping a script to check for posts made since the last ping~
query you db like this:
"select * post from posts where user_id=$uid order by post_id desc limit 0,20"
this will get the most recent 20 posts.
Assuming the 'post_id' field is an auto increment int, you can keep track of the largest value ID when you first load the page. For subsequent jquery ajax requests, just do a
SELECT * FROM posts WHERE user_id = '{$user_id}' AND post_id > '{$last_id}' ORDER BY post_id ASC
If the user is submitting posts on the same page that you want to update, why not just update the page as soon as the post is successfully submitted (via ajax)? You avoid re-fetching information you already know, and you avoid re-querying the database all together.
You would dynamically add a post to the page as it is submitted.
function onPostSubmitted(post){
// AJAX submit of the post
$("#posts").prepend( $("<div />").text( postData ) );
}