in wordpress we have add_query_arg() function to generat a url query from public key and value .
i retrive the cat link from query with add_query_arg() function like this :
$retrive ='424'; //my custom cat id .
$postnumber = '5';
$my_query = new WP_Query(array('showposts' => $postnumber , 'cat' => $retrive));
$query_name = get_cat_name($retrive);
$key = 'cat';
$value = $retrive;
// generat query link
$query_link= esc_url( add_query_arg( $key , $value ,site_url('/') ) );
echo '<a class="btn btn-lg btn-blue" href="'.$query_link.'">'.$query_name.'</a>';
now ,i want the link of "top" and "rand" post with add_query_arg() function.
this is my query's:
$top_query = new WP_Query(array('meta_key'=>'post_views_count','orderby'=>'meta_value_num','showposts'=>$postnumber,'order'=>'DESC'));
$key = ?;
$value = ?;
$rand_query = new WP_Query(array('showposts' => $postnumber , 'orderby' => 'rand'));
$key = ?;
$value = ?;
(i need this links for my custom tab_widget plugin that user can see the link of other post of current tab.)
how can do this ?
I am trying to add a list of email addresses to wp_mail() from a certain user role. I have a comma delimited list stored as $user_email_list but cannot get that to output into the $multiple_recipients array.
Any help would be greatly appreciated.
// Get users and their roles, create list of emails to send notification to.
$user_args = array(
'role__in' => 'test_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email_list = $user->user_email . ', ';
endforeach;
// Email Data
$multiple_recipients = array(
$user_email_list
);
$subject = $post->post_title;
$body = $post->post_content;
Update your code of foreach and check your $multiple_recipients variable at final, it would be comma separated value.
foreach ( $users as $user ) :
$user_email_list[] = $user->user_email;
endforeach;
$multiple_recipients = implode(', ', $user_email_list);
I'm trying to get all the comments from a post by an array of users.
This is what I'd like to be able to do:
$user_ids = array(10, 22, 41, 80);
$post_id = 57;
$args = array (
'number' => -1,
'user_id' => $user_ids,
'post_id' => $post_id,
'status' => 'approve',
'order' => 'DESC'
);
$comments = get_comments( $args );
Now obviously this doesn't work, but that's what I'd like to do. Is there any other way to achieve this? Maybe using a custom select?
I've built a WPDB query based on the query method of WP_Comment_Query class. And doing the sanitization based on this forum post.
global $wpdb;
// Sanitize
$post = '1148';
$post = absint($post);
// Sanitize
$a = '2'; // User One
$b = '3'; // User Two
$user_ids = array_map( 'absint', array( $a, $b ) );
$user_ids = implode( ', ', $user_ids );
$query = "SELECT * FROM $wpdb->comments
WHERE comment_post_ID = $post
AND user_id IN ($user_ids)
AND comment_approved = 1
ORDER BY comment_date DESC";
$comments = $wpdb->get_results( $query );
A simple SELECT query would do:
$query = "SELECT * FROM $wpdb->comments
WHERE comment_post_ID = %d
AND comment_approved = 1
AND user_id IN %s
ORDER BY comment_date DESC"; // get only approved comment and sort by date
$comments = $wpdb->get_results($wpdb->prepare(
$query,
intval($post_id),
'('.implode(',', array_map('intval', $user_ids)).')'
)); // use prepare to prevent SQL injection
Hope this helps.
We can use the OR condition in MySQL to do this.
If I was to write the query we want using the values you provided it could be done something like this:
SELECT * FROM comments WHERE comment_post_ID='57' AND (user_id='10' OR user_id='22' OR user_id='41' OR user_id='80')
Now, we can make this dynamic and processable by PHP:
// The User IDs and Post ID (make sure you are escaping these properly).
$user_ids = array(10, 22, 41, 80);
$post_id = 57;
foreach($user_ids as $uid){ $user_ids_string .= " OR user_id='$uid'"; } // Loop through each user and append their ID to the query segment.
$user_ids_string = substr($use, 4); // Remove the unnecessary first " OR "
$query = mysql_query("SELECT * FROM comments WHERE comment_post_ID='$post_id' AND ($user_ids_string)"); // Create the final MySQL Query.
while($row = mysql_fetch_array($query)){
// Do something with each $row[].
}
Before you use this, make sure you're connected to the WordPress database properly before using this and that all the tables and fields I've listed are correct for your installation first.
paste this code in functions.php
function users_comment( $postid = null , $users = array() ){
if( ! empty( $users ) && $postid ){
foreach ($users as $user) {
$args = array (
'number' => '',
'user_id' => $user,
'post_id' => $postid,
'status' => 'approve',
'order' => 'DESC'
);
$comments[] = get_comments( $args );
}
return $comments;
}else{
return 'Please provide a user id and postid';
}
}
use this function anywhere you want by calling it required parameters user ids and post id.
print_r( users_comment(1,array(1,4,3)) );
only single user to get post:
$args = array(
'status' => 'approve',
'number' => '-1',
'post_id' => 57,
'user_id' => 1,
'order' => 'DESC'
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;
?>
For multiple user get comment using post id:
$args = array( 'user_id' => 0 );
add_filter( 'comments_clauses', 'custom_comments_clauses');
$comments = get_comments( $args );
remove_filter( 'comments_clauses', 'custom_comments_clauses');
function custom_comments_clauses( $clauses ){
$clauses['where'] = str_replace( 'user_id = 0',
'user_id IN (1, 2, 3)',
$clauses['where'] );
return $clauses;
}
https://wordpress.stackexchange.com/questions/105010/get-comments-only-for-certain-specific-users-in-template-file
As Brasofilo already provided you the custom query to get the comments but it will fetch all the comments while they were trashed
$user_ids = array(10, 22, 41, 80);
$post_id = 57;
global $wpdb;
$comments=$wpdb->get_results("SELECT * FROM `wp_comments` WHERE
`comment_post_ID` =$post_id AND `user_id` IN (".join(',',$user_ids)")
AND `comment_approved` ='1' ORDER BY `comment_date` DESC");
I try to create a table where a row is looking like this:
|Text1|Text2|Button1|
As soon as the user clicks on the button I want to exchange the Button1 with tow textFields and another button... so a normal AJAX-request.
I've tried to implement this, not sure if this is the correct way, let me know if I could to it in a completely other way :S
So what I've already done:
In my hook_menu i link to a function that returns the data:
return theme ( 'my_theme_function', array (
'running' => true,
'form' => $database->getData ()
) );
What happens in the getData()-Function:
I create the form using drupal_get_form(..).. returned from the given form is this:
$data = array (
'items' => $finder->getGamesToPlayForm ( $form_state ),
'#title' => 'Title'
);
the $finder returns the list of items I want to show in the table. So nothing special.
my_theme_function is set to use a template-file where I would like to show the whole stuff.
This looks like this:
$header = array (
'Col1',
'Col2',
''
);
$rows = array ();
foreach ( element_children ( $formData ['items'] ) as $gameId ) {
$row = array ();
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#col1'] );
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#col2'] );
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#form'] );
$rows [] = $row;
}
$output = theme ( 'table', array (
'header' => $header,
'rows' => $rows
) );
unset($formData['items']);
$output .= drupal_render_children ( $formData );
print $output
this way, everything is printed correct. But there are no "form"-tags, which stops my form from working..
So, any idea/hint how to solve this kind of problem? Probably I'm on the completely wrong way..
try
'render element' => 'form'
in your theme
I have posts with meta data that I want to sort by 'LiveStreamDate'. Format of meta field is: yyyy/mm/dd.
My current code below:
$recent = new WP_Query('cat='.$spcatid.'&paged=' . $paged); while($recent->have_posts()) : $recent->the_post();
$tmpLiveTime = get_post_custom_values("LiveStreamTime");
$tmpLiveDate = get_post_custom_values("LiveStreamDate");
$tmpLiveCompetition = get_post_custom_values("LiveStreamCompetition");
$tmpLiveMatch = get_post_custom_values("LiveStreamMatch");
NORMAL LOOP STUFF HERE
Any ideas? I have looked through the WP_QUERY examples using meta fields and values - but how to construct the query (or add another query) to end up with data sorted by meta field by value of it's date?
Cheers
BK
You can use the follow example:
add_filter('posts_orderby', 'my_filter_posts_orderby' );
$query = new WP_Query( array(
'meta_key' => 'LiveStreamDate',
'cat' => $spcatid,
'paged' => $paged,
) );
remove_filter( 'posts_orderby', 'my_filter_posts_orderby' );
function my_filter_posts_orderby( $orderby )
{
global $wpdb;
$orderby = $wpdb->postmeta . '.meta_value DESC, ' . $orderby;
return $orderby;
}
while($query->have_posts()) {
$query->the_post();
echo get_post_custom_values("LiveStreamDate");
}
The list of possible parameters are in WordPress file
/wp-includes/query.php
inside this function:
/**
* Fills in the query variables, which do not exist within the parameter.
*
* #since 2.1.0
* #access public
*
* #param array $array Defined query variables.
* #return array Complete query variables with undefined ones filled in empty.
*/
function fill_query_vars($array) {
$keys = array(
'error'
, 'm'
, 'p'
, 'post_parent'
, 'subpost'
, 'subpost_id'
, 'attachment'
, 'attachment_id'
, 'name'
, 'static'
(...)