How to get data from WordPress Object? - wordpress

I want to get all user list of a specific category. I use WP_User_Query() function for the query and assign its returned value into a variable.When I use var_dump() then its show as like http://postimg.org/image/48st2h0jj/. How can I get the role of that user from that object. My code is below ..
$user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
foreach ($user_query as $user) {
$aa = $user->role;
echo $aa;
}
But It doesn't work as I want. How can I solve this problem.
Thanks...Anam

Try This
<?php
$args = array(
'role' => 'Administrator'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->role . '</p>';
}
} else {
echo 'No users found.';
}
?>
if it's Not Work Than Use This code
$administrator_query = new WP_User_Query(
array(
'role'=>'administrator',
)
);
$user_query= $administrator_query->get_results();
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->role . '</p>';
}

$user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
foreach ( $user_query as $user ) {
$value = json_decode(json_encode($user),true);
echo $value['role']."<br>";
}
json_decode function will convert it into array format and then we can access them

After some long search I find my own solution... At first I have to make object as an array. Then I get the result from that array... here is my own code
$user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
$posts = $user_query->get_results();
foreach ($posts as $user) {
$aa = $user->roles[0];
echo $aa;
}
Thanks to all for answer my question.

Related

How to insert new hidden postmeta on a published post ? WordPress

there are a hundred posts to update the postmeta if I do manually. I try to find a way to insert new postmeta based on the post_type and category. I try this:
$args = [
'post_type' => 'post',
'category_name' => 'category'
];
$query = new WP_Query($args);
if( $query->have_posts() ) {
while ( $query->have_posts() ) {
add_post_meta($query->post->ID, '_meta_key', meta_value, true);
add_post_meta($query->post->ID, '_meta_key2', meta_value2, true);
add_post_meta($query->post->ID, '_meta_key3', meta_value3, true);
add_post_meta($query->post->ID, '_meta_key4', meta_value4, true);
add_post_meta($query->post->ID, '_meta_key5', meta_value5, true);
}
}
and it is not working. is there anyway to achieve ?
OK I found the answer here Trigger action when new post is insert in wordpress
Add meta value when post or page or custom post published.
function save_post_meta( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "your post name" != $post_type ){ return;}
update_post_meta( $post_id, 'meta_key', 'value');
}
add_action( 'save_post', 'save_post_meta', 10, 3 );
Use update_post_meta function to update post meta values.
$args = [
'post_type' => 'post',
'category_name' => 'category'
];
$query = new WP_Query($args);
if( $query->have_posts() ) {
$query->the_post();
while ( $query->have_posts() ) {
update_post_meta($query->post->ID, '_meta_key', meta_value, true);
update_post_meta($query->post->ID, '_meta_key2', meta_value2, true);
update_post_meta($query->post->ID, '_meta_key3', meta_value3, true);
update_post_meta($query->post->ID, '_meta_key4', meta_value4, true);
update_post_meta($query->post->ID, '_meta_key5', meta_value5, true);
}
}

Wordpress filter post before displaying

Currently all posts is being listed by category,
i need to filter the list by a meta-key. I used the filter('posts_where') but all the query has been changed.
I need to add the WHERE condition in the existing sql that is being generated.
function get_filtered_post( $args, $meta, $value ){
$posts = get_posts( $args );
$ids = array();
foreach( $posts as $post ){
$id = $post->ID;
if( get_post_meta( $id, $meta, true ) == $value ){
$ids[] = $id;
}
}
return $ids;
}
$args = array( 'post_type' => 'post', 'posts_per_page' => -1 );
$IDofPost = get_filtered_post( $args, 'my-meta-key', 'the-metas-value' );
foreach( $IDofPost as $id ){
echo get_the_title( $id );
}
Hope this function will help. It returns post id with the given post meta values.

get_post_meta with save_post or publish_post action hook

I am trying to get_post_meta from wp_postmeta to copy to a plugin table wp_wpgmza when a post is published.
Using a save_post action hook correctly copies dat from the wp_post table but I get blank fields for wp_postmeta.
I have tried using a publish_post action hook but this also returns blanks.
This is the latest function I have been;
function map_update($ID, $post) {
//if ($post->post_type = 'post') return;
$link = get_permalink ( $ID );
$title = get_the_title ( $ID );
$mapaddress = get_post_meta ( $ID, 'address', true );
global $wpdb;
$table = $wpdb->prefix . 'wpgmza';
$data = array(
'link' => $link,
'title' => $title,
'address' => $mapaddress,
);
$wpdb->insert($table , $data);
}
add_action('publish_post', 'map_update' );
please check I cahnged argument to $post_id may be you get your solution
function map_update($post_id) {
$ID=$post_id;
//if ($post->post_type = 'post') return;
$link = get_permalink ( $ID );
$title = get_the_title ( $ID );
$mapaddress = get_post_meta ( $ID, 'address', true );
global $wpdb;
$table = $wpdb->prefix . 'wpgmza';
$data = array(
'link' => $link,
'title' => $title,
'address' => $mapaddress,
);
$wpdb->insert($table , $data);
}
add_action('publish_post', 'map_update' );

ordering wordpress post_meta - lowest to highest

I am currently looping through all posts and displaying a post_meta value like this:
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){
get_post_meta( $post->post_id, '_wholesale_price', false );
}
Is there a way to order the results, lowest -> highest? At the moment they are getting displayed randomly, or as they were entered.
use the following code
<?php
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
$all_post[] = $post->post_id;
}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
This looks a bit wrong to me. If you're trying to order posts by the value of a meta key (unless I'm mistaken, that is what you're doing) then I would normally go about it using WP_Query()
global $wp_query;
$args = array(
'post_type' => '[YOUR POST TYPE]',
'meta_key' => '_wholesale_price',
'orderby' => 'meta_value meta_value_num', // meta_value_num for value
'order' => 'ASC' // or DSC for low/high
);
$wp_query - new WP_Query( $args );
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// Your loop
}
}

List authors with gravatar and other data in wordpress?

How do I list authors with their gravatar, first namne last name and nickname in wordpress?
This is how I did it!
<?php
// Displays user name and email from users with at least one post
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$args = array(
'author' => $bloguser->user_id,
'showposts' => 1,
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$user = get_userdata($bloguser->user_id);
echo "<img src='http://www.gravatar.com/avatar/" . md5( strtolower( trim( " $user->user_email " ) ) )."?s=125' /><li>".$user->user_firstname."</li><li>".$user->user_lastname."</li>";
}
}
}
?>

Resources