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.
Related
Im looking to create a simple query but i can't seem to figure out how to do it using Cakes conditions (cake 1.3), what im looking to do is quite simple, in SQL its just
SELECT * FROM table WHERE firstvalue != '' OR secondvalue != ''
So basically only return row if there is a value in either firstvalue or second value.
At the moment i have;
$conditions = array(
"NOT" => array(
'firstvalue' => ''
)
);
This works fine for the first value, but if i try and add anything to it, it still only returns the results for the firstvalue
Two ways to go about this:
$conditions = array(
'OR' => array(
array('NOT' => array('firstvalue' => '')),
array('NOT' => array('secondvalue' => ''))
)
);
Or, take advantage of the fact that "NOT a OR NOT b" == "NOT (a AND b)":
$conditions = array(
'NOT' => array(
array('firstvalue' => ''),
array('secondvalue' => '')
)
);
To add multiple fields in OR condition, you have to write the fields in array.
Like,
$conditions = array(
"OR" => array(
"NOT"=>array('firstvalue' => ''),
array('secondvalue'='')
)
)
);
I hope it will work for you.
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'];
}
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
I have a table called items and a table called item_pics.
item_pics has an item_id, file_name and a rank field (among others).
What I'm looking for is for each item my index page's $items array to contain the file_name from the item_pics matching the item's item_id with the lowest rank. So I can access like (or something like) this in my Items/index.ctp:
foreach ($items as $item):
$img = $item['Item']['ItemPic']['file_name'];
...
I'm pretty new to CakePHP, this is my first project. I thought that this within the Item model would cause item_pics data to be pulled (although I figured all related item_pics for each item would get pulled rather than just the one with the lowest rank):
public $hasMany = array(
'ItemPic' => array(
'className' => 'ItemPic',
'foreignKey' => 'item_id',
'dependent' => false
)
}
but I can see that no item_pics data is loaded (at the bottom of items/index):
SELECT `Item`.`id`, `Item`.`title`, `Item`.`description`, `Item`.`created`, `Item`.`modified`, `Item`.`type`, `Project`.`id`, `Project`.`item_id`, `Project`.`title`, `Project`.`description`, `Project`.`rank`, `Project`.`created`, `Project`.`modified`
FROM `laurensabc`.`items` AS `Item`
LEFT JOIN `laurensabc`.`projects`
AS `Project`
ON (`Project`.`item_id` = `Item`.`id`)
WHERE `Item`.`type` IN (1, 2)
LIMIT 20
also, while I would like projects to be joined in the view pages, I don't really need them in the index page.
I've done some searching and haven't been able to find exactly what I'm looking for. I suppose I could do a query within the index view item loop, but I'm trying to make sure I do things the right way... the CakePHP way. I assume I need to change something about my model relationships but I haven't had any luck.
CakePHP - Associations - HasMany, this makes it seem like I could order by rank and limit 1. But this didn't work... and even if it did, I wouldn't want that to affect the view pages but rather just the index page.
My Controller looks like this:
public function index($type = null) {
$this->Item->recursive = 0;
$conditions = array();
if ($type == "sale") {
$conditions = array(
"Item.type" => array(self::FOR_SALE, self::FOR_SALE_OR_RENT)
);
} else if ($type == "rent" ) {
$conditions = array(
"Item.type" => array(self::FOR_RENT, self::FOR_SALE_OR_RENT)
);
} else {
$conditions = array("Item.type !=" => self::HIDDEN);
}
$paginated = $this->Paginator->paginate($conditions);
debug($paginated);
$this->set('items', $paginated);
$this->set('title', ($type == null ? "Items for Sale or Rent" : "Items for " . ucwords($type)));
}
I have also tried this on my controller, but it doesn't seem to do anything either:
$this->paginate = array(
'conditions' => $conditions,
'joins' => array(
array(
'alias' => 'ItemPic',
'table' => 'item_pics',
'type' => 'left',
'conditions' => array('ItemPic.item_id' => 'Item.id'),
'order' => array('ItemPic.rank' => 'asc'),
'limit' => 1
)
)
);
$paginated = $this->paginate($this->Item);
First, set containable behavior in AppModel (or if you don't want it on each model, put it on Item model):
public $actsAs = array('Containable');
Then, on your find query:
$items = $this->Item->find('all', array(
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
));
Then the result array you can access it like:
foreach ($items as $item):
$img = $item['ItemPic']['file_name'];
Edit: Then you should put it on the paginate query:
$this->paginate = array(
'conditions' => $conditions,
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
);
In this case, I would probably order by rank and limit 1 as you said, and make that a dynamic association just for the index page (See http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly). So use $this->Item->bindModel(array('hasMany' => array('ItemPic' => $options))); (which I believe should replace your current settings for HasMany ItemPic, but you may have to unbindmodel first)
Associations created through bindModel will go through for the next query only, then it'll revert to your normal settings, unless you specifically set an option to keep using the new association.
As for why it's not getting ItemPics with Items, or why trying to order by rank and limit 1 didn't work for you, I can't really say without seeing more of your code.
I'd like to paginate some data and order them using FIND_IN_SET. If there is only one param it works setting the order like so:
$this->paginate['order'] = 'FIND_IN_SET(Catalog.pseudonym_id, "197,109,687")';
In CakePHP2, when there is more then one order param, they have to be entered as key => value. How can this be done with FIND_IN_SET? The following don't work
array(
(int) 0 => 'FIND_IN_SET(Catalog.pseudonym_id, "197,109,687")',
'Catalog.catalog_type_id' => 'ASC',
'Edition.year' => 'asc'
)
array(
'FIND_IN_SET' => '(Catalog.pseudonym_id, "197,109,687")',
'Catalog.catalog_type_id' => 'ASC',
'Edition.year' => 'asc'
)
array(
'FIND_IN_SET (Catalog.pseudonym_id, "197,109,687")' => 'ASC',
'Catalog.catalog_type_id' => 'ASC',
'Edition.year' => 'asc'
)
Any ideas? work around?
The way to have it work, is to set the order param as a string and not using the array, and then it's ok, as follow:
$this->paginate['order'] = 'FIND_IN_SET(Catalog.pseudonym_id, "197,109,687"), Catalog.catalog_type_id ASC, Edition.year ASC';