get_users() when meta_key has multiple serialized values - php

I can't seem to find any information online, but what I am trying to do is to get an array of all users based on a value in a serialised meta_key, for example:
$get_users = get_users(array(
'meta_key' => 'value_one_of_array'
));
Then in the db, the meta_value columns would have two stored values such as:
meta_key
value_one_of_array , value_two_of_array
Is this possible?

May be it's help you.
$args = array();
if($values_of_array){
foreach($values_of_array as $key=>$val){
$args['meta_query'][] = array(
'key' => '#your_meta_key',
'value' => $val
);
}
$get_users = get_users($args);
}

Related

Name keys within array from WordPress meta fields

i am trying to code a calendar script within wordpress and custom post types. works fine so far. I use several custom fields (ACF) to call my desired parameters (event title, start_date, end_date, etc.)
I call the results by get_posts().
but I have a problem with naming my keys within the array. after the foreach operation I can get the desired values e.g. event_title by typing $posts[0] or start_date by $posts['1'].
how can I achieve that I can name the keys so that I can use $posts[title] or $posts['start_date'] in my output?
I have tried to build a second array an use array_combine, but this fails.
<?php
$args = array( 'post_type' => 'cpt_kalender', 'post_status' => 'publish', 'numberposts' => '-1', 'meta_key' => 'cf_termin_start', 'orderby' => 'meta_value_num', 'order' => 'ASC' );
$posts = get_posts( $args );
$group_posts = array();
foreach ( $posts as $post ) : setup_postdata( $post );
/*
Hole die Termine der einzelnen Startdaten
- Schreibe das Datumsformat yyyymmdd in yyyy-mm-dd um
*/
$titel = get_the_title();
$termin_datum_start = get_field('cf_termin_start');
$termin_datum_start = new DateTime($termin_datum_start);
$termin_datum_ende = get_field('cf_termin_ende');
$termin_datum_ende = new DateTime($termin_datum_ende);
$termin_jahr = $termin_datum_start->format('Y');
$termin_monat = $termin_datum_start->format('F');
$termin_tag = $termin_datum_start->format('d');
$post_id = get_the_ID();
$termin_start_uhrzeit_stunde = get_field('cf_termin_start_uhrzeit_stunde');
$termin_start_uhrzeit_minute = get_field('cf_termin_start_uhrzeit_minute');
$termin_ende_uhrzeit_stunde = get_field('cf_termin_ende_uhrzeit_stunde');
$termin_ende_uhrzeit_minute = get_field('cf_termin_ende_uhrzeit_minute');
$termin_schulfrei = get_field('cf_termin_schulfrei');
$termin_intern = get_field('cf_termin_intern');
/*
Fülle das Array mit den Werten
*/
$group_posts[$termin_jahr][$termin_monat][] = array(
$titel,
$termin_datum_start,
$termin_datum_ende,
$post_id,
$termin_start_uhrzeit_stunde,
$termin_start_uhrzeit_minute,
$termin_ende_uhrzeit_stunde,
$termin_ende_uhrzeit_minute,
$termin_schulfrei,
$termin_intern
);
endforeach; ?>
Is there any way to tell the array listed above the desired key names in the same foreach operation?
i am thankful for every hint. thx a lot. I am just learning php and this drives me nuts. :(
The part where you are adding the values to the $group_posts array. You can add keys to each item.
$group_posts[$termin_jahr][$termin_monat][] = array(
'title' => $titel,
'termin_datum_start' => $termin_datum_start,
'termin_datum_ende' => $termin_datum_ende,
'post_id' => $post_id,
'termin_start_uhrzeit_stunde' => $termin_start_uhrzeit_stunde,
'termin_start_uhrzeit_minute' => $termin_start_uhrzeit_minute,
'termin_ende_uhrzeit_stunde' => $termin_ende_uhrzeit_stunde,
'termin_ende_uhrzeit_minute' => $termin_ende_uhrzeit_minute,
'termin_schulfrei' => $termin_schulfrei,
'termin_intern' => $termin_intern
);
Then you can access a value like $group_posts['termin_jahr']['termin_monat']['title']
Since you are adding each item with termin_jahr and termin_monat You will need to provide those first to access the correct item's title.
Alternatively you can just add those variables inside of the array and access it later like below:
$group_posts = array(
'title' => $titel,
'termin_datum_start' => $termin_datum_start,
'termin_datum_ende' => $termin_datum_ende,
'post_id' => $post_id,
'termin_start_uhrzeit_stunde' => $termin_start_uhrzeit_stunde,
'termin_start_uhrzeit_minute' => $termin_start_uhrzeit_minute,
'termin_ende_uhrzeit_stunde' => $termin_ende_uhrzeit_stunde,
'termin_ende_uhrzeit_minute' => $termin_ende_uhrzeit_minute,
'termin_schulfrei' => $termin_schulfrei,
'termin_intern' => $termin_intern,
'termin_jahr' => $termin_jahr,
'termin_monat' => $termin_monat,
);
You can do a foreach loop on the $group_posts array and then access each item like below:
foreach($group_posts as $group_post) {
// Outputs $titel variable which you added to array above
echo $group_post['title'];
}

how to use IN query in cakephp to find multiple values

I'm using LIKE query but when specialist_id's order change it say no records found,
if specialist_id array have [1,2,3] in it then all the users with three specialties should be in results regardless of sequence, sequence array may have [3,2,1] or [2,1,3] but the users with these three specialties should be in results, here is my code with LIKE query:
$field = $this->User->find(
'all', array(
'conditions'=>array(
'User.specialist_id LIKE' =>'%'.$value.'%',
'User.role'=>'careproviderRole',
'User.gender'=>$this->request->data['gender'])
)
);
foreach ($field as $key => $value)
{
$field[$key]['Specialist'] = $user;
}
I have also tried with FIND_IN_SET:
$field = $this->User->find(
'all', array(
'conditions' => array(
'User.role' => 'careproviderRole',
'FIND_IN_SET(\''.$this->request->data['specialist_id'].'\', User.specialist_id)')
)
);
When you use LIKE with mask '%some%', it find all results with substring 'some'.
If you need to get some specific set of ids from table (1, 2 or 3), then use sql operator IN.
Example:
SELECT `specialist_id` FROM `User` WHERE `specialist_id` IN (1,2,3);
In your code it will be:
$field = $this->User->find(
'all', array(
'conditions'=>array(
'User.specialist_id' => $arrayOfSpecialistIds,
'User.role'=>'careproviderRole',
'User.gender'=>$this->request->data['gender'])
)

Extracting optionValue from postmeta

I need to find all addresses from a wp database so I select all rows having metakey = mkd_portfolios having opionLabel = Address from postmeta table.
But I can't find how to extract items in meta_value.
I guess it is something easy with php?
meta_value : a:1:{i:0;a:4:
{s:11:""optionLabel"";s:7:""Address"";s:11:""optionValue"";s:21:""Dune du Pilat,
France"";s:9:""optionUrl"";s:0:"""";s:22:""optionlabelordernumber"";s:1:""1"";}}
meta_id: 15361
post_id: 11428
There is the WP_Meta_Query class.
$meta_query_args = array(
array(
'key' => 'optionLabel',
'value' => 'Address',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
You might want to further narrow down your results via the AND relation in the query, I can not tell from the code you posted. Find more info at the link provided.

Wordpress - Update post by meta information

I'm looking for a solution, where I can update posts by meta information and not by id.
I got my content from an external resource via jsonp. I save the content into the database.
The only way I have found is, to update a post by id - but I have no id. What I have, is an article ID from the other resource. I save this article id to every wp post as meta information.
So, is there any way, to update a post by it's meta information?
Ok,
here is some code and what I have done right now:
public function saveArticleInWpDatabase($json_data){
for ($i = 0; $i < count($json_data); $i++) {
$articleId = $json_data[$i]['articleId'];
$img = $json_data[$i]['img'];
$articleHeadline = $json_data[$i]['articleContent']['headline'];
$articleContent = $json_data[$i]['articleContent']['content'];
$termsInput = $json_data[$i]['articleContent']['terms'];
$terms = explode(',',$termsInput);
$creationDate = $json_data[$i]['articleCreationDate'];
$modifiedDate = $json_data[$i]['articleModifiedDate'];
//Check if post with article ID in postmeta exists
$existingId = $this->wpdb->get_var("SELECT post_id FROM ".$this->wpdb->postmeta." WHERE (meta_key = 'article_id' AND meta_value = '".$articleId."')");
//Posts doesn't exist
if ($existingId == null) {
$my_post = array(
'post_date' => $creationDate,
'post_modified' => $modifiedDate,
'post_title' => $articleHeadline,
'post_content' => $articleContent,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(0, $categoryId)
);
$last_id = wp_insert_post($my_post);
add_post_meta($last_id, 'article_id', $articleId, true);
wp_set_post_categories($last_id, array(0,$categoryId), true);
wp_set_post_terms( $last_id,$terms, 'post_tag', true );
//$this->_import_photo($last_id, $img);
}
//Post exists
else{
$my_post = array(
'ID' => $existingId,
'post_date' => $creationDate,
'post_modified' => $modifiedDate,
'post_title' => $articleHeadline,
'post_content' => $articleContent,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(0, $categoryId)
);
//Get last modified date and compare it with the new one
$lastUpdate = $this->wpdb->get_var("SELECT post_modified FROM " . $this->wpdb->posts . " WHERE ID = ".$existingId);
if(strcmp($lastUpdate, $modifiedDate) != 0){
var_dump("Update article with id: " . $existingId);
$last_id = wp_update_post($my_post);
}
else{
var_dump("Article: " . $existingId . " Nothing changed");
}
}
}
}
As you can see, I save the unique article number from the other CMS if the article doesn't exists.
If the article with the meta_key: article_id and the current value exist, I check if the modified date is the same or not. If not, I will update the posts.
I call this function via a unix cron job every minute. But I think, that this is not the best way (performance)(too many db requests?).
The other think is, that the post_modified value is always the same as post_date, but I got two different values in my json.
e.g.
"articleCreationDate": "2012-05-21 14:38:29", "articleModifiedDate": "2016-02-11 14:52:01"
But it only saved the articleCreationDate in both columns(post_date, post_modified).
So, did anyone has a better solution or idea? And did anyone know, why it doesn't save the modified date value from my json?
Cheers

PHP. How to take data from 2 mysql tables instead of 1

Learning php and I am losing my mind trying to solve this for days now. Please help.
This is a code which goes thought a table COUPON, take data with a condition met, and download it afterwards. In this table COUPON I have USER_ID as number but I want to have a user name also, which is kept in another table USER.
How can I go to another table (USER) and take names (REALNAME) by this USER_ID which is the same in both tables?
if ( $_POST ) {
$team_id = abs(intval($_POST['team_id']));
$consume = $_POST['consume'];
if (!$team_id || !$consume) die('-ERR ERR_NO_DATA');
$condition = array(
'team_id' => $team_id,
'consume' => $consume,
);
$coupons = DB::LimitQuery('coupon', array(
'condition' => $condition,
));
if (!$coupons) die('-ERR ERR_NO_DATA');
$team = Table::Fetch('team', $team_id);
$name = 'coupon_'.date('Ymd');
$kn = array(
'id' => 'ID',
'secret' => 'Password',
'date' => 'Valid',
'consume' => 'Status',
);
$consume = array(
'Y' => 'Used',
'N' => 'Unused',
);
$ecoupons = array();
foreach( $coupons AS $one ) {
$one['id'] = "#{$one['id']}";
$one['consume'] = $consume[$one['consume']];
$one['date'] = date('Y-m-d', $one['expire_time']);
$ecoupons[] = $one;
}
down_xls($ecoupons, $kn, $name);
After this, I want to try to do the same thing using only SQL queries.
You would need to JOIN the tables in the SQL query
SELECT something FROM coupons as coupons JOIN user as user ON coupons.id=user.id
You should use join when you want to retrieve details from two tables.
Join table COUPON and table USER based on user_id . This should yield results you want.

Categories