Tagged: ajax foreach error widget
- This topic has 8 replies, 2 voices, and was last updated 10 years ago by traversal.
Author | Posts |
---|---|
November 7, 2014 at 9:16 am #3561 | |
sitesurety
Post count: 13
|
hi again, i’ve been converting some of the data code in my template into function within the functions.php file. I’ve found that when I use ajax to request the data, I get a for loop error: When I load the page initially using the short code, it loads fine. In the template I have the following code: My ajax call, calls my_action_callback() below, which should return the flight data. In my functions.php file I have: function my_action_callback() { // Get and sanitise variables // Clear existing filters $filter = array( echo returnTable(); which then calls: function returnTable() { $string = “”; $fieldset = $post->custom-post-type; return $string; I initially had the ajax calls working, but not the other way around. This seemed to work when I replaced the Masterpress for loop with the standard call, and a global $post variable and query in the returnTable() function: $args = array( |
November 7, 2014 at 10:28 am #3562 | |
traversal
Post count: 207
|
Hey there. Yeah, I suspect this doesn’t work because there’s no notion of the “current post” or the current query when using the AJAX filters, so the call to $wf->the, or $wf->loop fails. I suspect you’d have to pass the post ID you’re looking for. $wf->loop will only work when you’ve got a global query object representing the current query for an archive page. If you’re trying to loop over the posts for a specific custom post type, you’d need to use: foreach ($wf->type(“custom_post_type”)->posts as $post) which should still work in an AJAX handler, as it doesn’t reply on global state. You can also shorten this and use: foreach ($wf->custom_post_types as $post) which will look for a post type based on the singular form of “custom_post_types”. That actually returns a WOOF_PostType object, not a collection, but WOOF_PostType implements the Iterator interface, and will return a collection of posts in that type when used in conjunction with foreach. |
November 7, 2014 at 11:08 am #3563 | |
sitesurety
Post count: 13
|
Thanks. Yes thought that may be the case. The way I’m using the data is a collection of objects (archive page), rather than individual posts . This is true at all times, so I won’t have a specific post ID… I think that is where I’ve been running into a few issues.. I’ll try explicitly setting the post query to retrieve the full list of posts… |
November 7, 2014 at 11:38 am #3564 | |
sitesurety
Post count: 13
|
Changing the loop code works. Is there a master press friendly way to set the $wf variable similar to: $flight_posts = new WP_Query($args); |
November 7, 2014 at 11:48 am #3565 | |
sitesurety
Post count: 13
|
I think I may have solved by own question with a big of debugging: foreach ($posts as $post) { Then I can use the values. |
November 7, 2014 at 11:58 am #3566 | |
sitesurety
Post count: 13
|
OK, one last problem hopefully related to this. With my query set to get a list of posts, and not the collection of fields.. the filter doesn’t work.. says method doesn’t exist. $query = $wf->the->custom_data_type->filter($filter_query_masterpress); So in order to set the initial query with a collection of the datatype, what query would I use? Currently I’m using $posts = $wf->type(‘custom_data_type’)->posts(); I think once that is solved, the function should work with Ajax and initial load, because the context of post will always be set to retrieve the entire collection of objects. |
November 7, 2014 at 4:48 pm #3567 | |
sitesurety
Post count: 13
|
OK, seems I’ve solved it. The following outputs the filtered field set.. foreach ($wf->loop() as $the) { foreach ($the->field_type->filter(“hair=brown&eyes=blue”) as $person) { } I’m noticing though it doesn’t filter based on both matches.. It will include any result where one of the filters matches. Is this expected, or is there a work around to filter combined? |
November 8, 2014 at 3:30 pm #3568 | |
sitesurety
Post count: 13
|
ok, i’ve gone through the filter code and it will include any match.. I’ve reversed the loop so that it will do an ‘and’ match (1 or more must be true to be added). Any chance this could be added in a future release as a variable to the function? i.e. Exclusive filter function filterExclusive($args, $case_sensitive = FALSE) { if (!is_array($args)) { // extract the arguments foreach ($this->_items as $set_item) { foreach ($args as $arg) { $m = array(); preg_match ( “/^([\w\-]+)(\#?\=|\#?[\>\<\$\^\*\~\!]=|\#?\>|\#?\<)(.*)$/” , $arg, $m ); if ($m && count($m) == 4) { if ($case_sensitive) { $f = $set_item->f($fn); $fields = array(); $fields[] = $f; foreach ($fields as $f) { if ($include) { $nv = 0; if ($case_sensitive) { if (is_numeric($fv)) { if (is_numeric($val)) { // Check each item for match individually switch ($op) { } // endif $include } // endforeach } } if ($include) { } } |
November 10, 2014 at 2:09 pm #3569 | |
traversal
Post count: 207
|
Awesome, thanks for the code. It’s definitely useful to be able to make this more fine-grained. The default behaviour really should be to use the intersection (AND) of the arguments rather than union (OR). I’ll note this does for a future release. |
You must be logged in to reply to this topic.