Name keys within array from WordPress meta fields - php

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'];
}

Related

get_users() when meta_key has multiple serialized values

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);
}

Prestashop filter products by category using WebService

I'm trying to use Prestashop WebService via PHP to filter products by categories but it seems that it's impossible.
How to make it? Must be something like this
array(‘resource’ =>’products’, ‘display’ => ‘[name]’, ‘filter[category]’ => ‘[x]');
Which Prestashop version do you use ?
I managed to get products for a specific category for v1.5.6.1 as follows:
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
$opt = array(
'resource' => 'products',
'display' => 'full',
'filter[id_category_default]' => '[8]',
'limit' => '5'
);
$xml = $webService->get($opt);
$resources = $xml->products->children();
At this stage you get a products collection. You can reach properties using standard object notation ..
$xml->categories->category->associations->products->product
foreach ( $resources as $key => $value ) :
echo $value->id; // product's identifier
echo $value->price; // product's .. guess what !
endforeach;
You should be able to see elements exposed by reaching YOUR_SITE/api/products?schema=synopsis
That's fine but I've not been able to retrieve products urls yet in order to print anchors.. Anyone ? Any suggustion ?
Complete documentation (1.5) here
Hope it will help.
EDIT
Construct products URLS on the fly
Make a first API call to retrieve categor(ies/y) you want and their
datas (url slug, ids of products they own, ...)
Make a second API call to retrieve the actual datas corresponding to ids retrieved during the first step.
Slugs are available under the link_rewrite property of items collections (like categories and products). There will be as many slugs as the total of languages that have been configured from the back-end, so you may want to loop over the link_rewrite property to get them all and build all urls.
## Initialize Prestashop API
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
## Getting category I want
$opt = array(
'resource' => 'categories',
'display' => 'full',
'filter[id]' => '[70]', # we are interested only in one category
'limit' => '1'
);
$xml = $webService->get($opt);
$ws_cat = $xml->categories->category;
$products = $ws_cat->associations->products->product;
## Gathering products ids to feed the second API call filter parameter
$productsIds = array();
foreach ( $products as $p ) {
$productsIds[] = (int)$p->id;
}
## Getting products ..
$opt = array (
'resource' => 'products',
'display' => 'full',
'filter[id]' => '['.implode('|',$productsIds).']',
'limit' => '4'
);
$xml = $webService->get($opt);
$products = $xml->products->product;
if ( count($products) ) {
$products = array();
foreach ( $products as $value ) {
$products[] = array(
'id' => $value->id
,'catalogURL' => "{$prestashop['url']}/{$ws_cat->link_rewrite->language[0]}/{$value->id}-{$value->link_rewrite->language[0]}.html";
);
# There you go ..
}
}

PHP store foreach query results in array in specific format

I'm trying to store the results of a query in an arrow, but need to do so in a specific format (I think).
The required format I need the results to be in is as follows:
'screenshots' => 'Plugin Screenshots',
This is what I have so far, along with my failed attempt to store the results:
$my_fake_pages = array();
$args = array(
'parent' => 8,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
$my_fake_pages[] = $category->slug . '=>' . $category->slug;
}
What troubles me most is I do not understand how I would go about getting the => in there, as without the inverted commas dreamweaver throws up PHP errors.
This is what it would look like normally:
$my_fake_pages = array(
'installation' => 'Plugin Installation',
'usage' => 'Plugin Usage',
'screenshots' => 'Plugin Screenshots',
'changelog' => 'Plugin Changelog',
'feedbacks' => 'Users\' Feedbacks',
);
Any help is appreciated.
foreach($categories as $category) {
$my_fake_pages[$category->slug] = $category->slug;
}
print_r($my_fake_pages);
The notation 'something' => 'another thing' means it's a key/value pair. So all you need to do is change your assignment line to something like $my_fake_pages[$category->slug] = $category->name.
See http://php.net/manual/en/language.types.array.php#language.types.array.examples

Wordpress JSON API

Hey guys i have been so far in luck with the JSON API i love it but i encouter this little problem with it comes to making multiple query in the same custom function.
global $json_api;
$result = array();
$category = $json_api->introspector->get_categories();
foreach($category as $value){
if($value->id !== 1 && $value->id !== 69 && $value->id !== 68 && $value->id !== 66){
$search_args = array(
'cat' => $value->id,
'order' => 'DESC',
'post_status' => 'publish'
);
$search_limits = array(
10 => array(
'limit' => 4
),
5 => array(
'limit' => 2
),
3 => array(
'limit' => 3
)
);
$json_api->query->count = ($search_limits[$value->id]['limit'] === null ? 1 : $search_limits[$v$
$result['posts'][$value->id] = $json_api->introspector->get_posts($search_args,true);
}
}
return $result;
i hardcoded some of the values just to make it more straight foward, the issue is that when i make one query i get value ID lets say [10330][10218][10202] according to the category im looking for. Then i make this same query but with another category ID. But same result happens. So i was wondering is due to some sort of caching in the API if so can i turn it off for this query ?
Thanks in advance :D
Well what do you expect the issue is that i was not reseting my queries apparently.
wp_reset_query();
did the trick.

CakePHP this->paginate not using order field

No matter what I do I can't get it to respect the order I specify.
$this->paginate = array(
'Car' => array(
'limit' => 6,
'order' => array(
'Car.year' => 'desc'
),
'table' => 'cars'
)
);
Generated SQL:
SELECT `Car`.`id`, ... `Car`.`year`,... FROM `cars` AS `Car` WHERE 1 = 1 LIMIT 6
Turns out I was using a :sort in my url parameters. Once I took that out all was well :)
If you only have one item, you don't need/shouldn't use an array:
//one thing
var $order = "Model.field DESC";
//multiple things
var $order = array("Model.field" => "asc", "Model.field2" => "DESC");
(per this page)
Note too that virtual fields are ignored by default when paginating.
See "Control which fields used for ordering" in Cake 2.0 Pagination documentation.
Example:
$this->MyModel->virtualFields['count'] = 0;
$this->Paginator->settings = array(
'fields' => 'COUNT(id) AS MyModel__count',
'group' => ('MyModel.group_id'),
'order' => array('MyModel__count' => 'DESC'),
);
// IMPORTANT: pass sortable fields including the virtual field as 3rd parameter:
$log = $this->Paginator->paginate('MyModel', null, array('MyModel__count', 'id'));

Categories