I have the following code which gives me a query generated , what I need it is to walk the array ( php foreach ) to query the data.
I tried to do with the classic foreach (php) but I foreach throws an error.
Thanks in advance...
Array (
[0] => stdClass Object
(
[_new] =>
[_attributes] => Array
(
[id] => 36
[nombre] => Diana
[estado] => 1
)
[_related] => Array
(
)
[_c] =>
[_pk] => 36
[_alias] => t
[_errors] => Array
(
)
[_validators] =>
[_scenario] => update
[_e] =>
[_m] =>
)
[1] => stdClass Object
(
[_new] =>
[_attributes] => Array
(
[id] => 1
[geoCode] => MAX
)
[_related] => Array
(
)
[_c] =>
[_pk] => 1
[_alias] => t
[_errors] => Array
(
)
[_validators] =>
[_scenario] => update
[_e] =>
[_m] =>
))
As I walk or convert to be read as ?
<?php foreach ($response as $value) {echo $value->nombre;} ?>
Related
I have loop in functions.php with this code:
$meta_query[] = array(
'key' => '$key',
'value' => $value,
'compare' => 'IN',
);
and i trying to add next $meta_query after loop but with "<" in compare. When I add 'IN' it's working but when i change to '>=" or ">" it doesn't work.
$meta_query[] = array(
'key' => 'price',
'value' => $value,
'compare' => '>=',
);
Do you know why?
This is my fullcode:
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'field_1' => 'type',
'field_2' => 'price',
'field_3' => 'type2',
'field_4' => 'city',
'field_5' => 'price_min',
'field_6' => 'price_max',
);
function my_pre_get_posts( $query ) {
global $post;
global $wpdb;
$parametry = get_field_objects($post->ID);
if(!is_single() && $post->ID != 2 && $query->get( 'cat' )){
/* start filter*/
$meta_query = $query->get('meta_query');
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
if($name == 'price_min'){
$meta_query[] = array(
'key' => 'price',
'value' => $value,
'compare' => '>=',
);
}
elseif($name == 'price_max'){
$meta_query[] = array(
'key' => 'price',
'value' => $value,
'compare' => '<=',
);
}
else{
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
}
}
// update meta query
$query->set('meta_query', $meta_query);
In URL (and in table wp_postmeta) i have: type, type2, price and city. I don't have price_min and price_max so I want operate on price field.
This is my working array after filtering:
Array ( [0] => Array ( [key] => type [value] => Array ( [0] => flat) [compare] => IN ) [1] => Array ( [key] => city[value] => Array ( [0] => Cracow) [compare] => IN ) )
and this is not working array when I add price_min and price_max:
Array ( [0] => Array ( [key] => type [value] => Array ( [0] => flat) [compare] => IN ) [1] => Array ( [key] => city [value] => Array ( [0] => Cracow) [compare] => IN ) [2] => Array ( [key] => price [value] => Array ( [0] => 50000 ) [compare] => >= ) [3] => Array ( [key] => price [value] => Array ( [0] => 300000 ) [compare] => <= ) )
It's likely that the $value is not an integer so trying to compare the value of it as an integer when it's a string will cause some issues. Before your WP_Query arguments, place this code in your file:
$value = (int) $value;
Take a look at type juggling in PHP for more info.
This is the documentation for WP_Query meta comparisons.
Is there any option that I can further filter post list for a custom post type in admin based on user_id if the same has been passed as a query string like http://album.multibaselocal.com/wp-admin/edit.php?post_type=albums&user_id=31 ?
Here is what I want to accomplish:
I have a custom post type called album which is used by site users to upload Albums to the system. By default when I go to the list page all albums come up fine. In user list page, I have added a custom column which shows a count of Published albums by each user which links back to album list page with a query string user_id like above.
I want to filter the albums by the user_id I have passed. Is this possible?
I am trying this:
add_filter( 'parse_query', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$requery = array(
'post_type' => 'albums',
'post_author' => $user_id,
'status' => 'publish',
'posts_per_page' => -1,
);
// What to do here?
}
}
I have lost here completely. Not an experienced WP developer!
If I have not understood it all wrong, because the hook I am using is called parse_query, I have to somehow parse the query again and filter the existing result using user_id. But don't know where and how.
EDIT (Query output):
WP_Query Object
(
[query] => Array
(
[post_type] => albums
[posts_per_page] => 20
)
[query_vars] => Array
(
[post_type] => albums
[posts_per_page] => 20
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] => user_id
[meta_value] => 31
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] => 1
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] => 1
[query_vars_hash:WP_Query:private] => 1c0b485390fc9607d8008a14177bd63d
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
EDIT (Function body presently using):
add_action( 'pre_get_posts', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if(!is_admin())
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
$query->set('meta_key', 'user_id');
$query->set('meta_value', $user_id);
}
echo '<pre>'; print_r($query); echo '</pre>'; die();
}
This reminded me of a similar problem I had a couple weeks ago. I was wanting to adjust the 'orderby' part of the query, but I believe you can accomplish your task with the same action. Try using the 'pre_get_posts' action instead of that filter. I haven't tested this, so let me know if it works!
CORRECT ANSWER (EDITED after user input):
function user_id_filter( $query ) {
if ( ! is_admin() && !$query->is_main_query() )
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$query->set( 'author', $user_id );
}
//Debug if necessary
//echo '<pre>'; print_r($query); echo '</pre>'; die();
}
add_action( 'pre_get_posts', 'user_id_filter', 500000000 );
I used this as a reference. https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
I integrated Mangopay plugin into my wordpress site for crowdfunding
After configuring it and when i try to purchase using mangopay interface i got these errors.
exception 'MangoPay\ResponseException' with message 'Bad request. One or several required parameters are missing or incorrect. An incorrect resource ID also raises this kind of error.' in /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/restTool.inc:272
Stack trace:
#0 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/restTool.inc(126): MangoPay\RestTool->CheckResponseCode(Object(stdClass))
#1 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/restTool.inc(93): MangoPay\RestTool->RunRequest()
#2 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/apiBase.inc(157): MangoPay\RestTool->Request('/users/natural', 'POST', Array)
#3 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/apiUsers.inc(24): MangoPay\ApiBase->CreateObject('users_createnat...', Object(MangoPay\UserNatural))
#4 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/gateway/class-wm-fonctions-gateway.php(65): MangoPay\ApiUsers->Create(Object(MangoPay\UserNatural))
#5 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/gateway/class-wm-fonctions-gateway.php(43): WM_Fonction_Gateway::wm_create_mangopay_user(Object(WP_User))
#6 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/gateway/class-wm-woocommerce-gateway.php(104): WM_Fonction_Gateway::wm_get_mangopay_user(Object(WP_User))
#7 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(646): WM_Woocommerce_Gateway->process_payment(1804)
#8 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/woocommerce/includes/class-wc-ajax.php(369): WC_Checkout->process_checkout()
#9 [internal function]: WC_AJAX::checkout('')
#10 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-includes/plugin.php(496): call_user_func_array(Array, Array)
#11 /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-admin/admin-ajax.php(85): do_action('wp_ajax_woocomm...')
#12 {main}MangoPay\ResponseException Object
(
[_responseCodes:MangoPay\ResponseException:private] => Array
(
[200] => OK
[206] => PartialContent
[400] => Bad request
[401] => Unauthorized
[403] => Prohibition to use the method
[404] => Not found
[405] => Method not allowed
[413] => Request entity too large
[422] => Unprocessable entity
[500] => Internal server error
[501] => Not implemented
)
[_errorInfo:MangoPay\ResponseException:private] => MangoPay\Error Object
(
[Message] => One or several required parameters are missing or incorrect. An incorrect resource ID also raises this kind of error.
[Errors] => stdClass Object
(
[AuthorId] => The AuthorId field is required.
)
)
[RequestUrl] => https://api.sandbox.mangopay.com/v2/rota90/payins/card/web/
[message:protected] => Bad request. One or several required parameters are missing or incorrect. An incorrect resource ID also raises this kind of error.
[string:Exception:private] =>
[code:protected] => 400
[file:protected] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/restTool.inc
[line:protected] => 272
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/restTool.inc
[line] => 126
[function] => CheckResponseCode
[class] => MangoPay\RestTool
[type] => ->
[args] => Array
(
[0] => stdClass Object
(
[Message] => One or several required parameters are missing or incorrect. An incorrect resource ID also raises this kind of error.
[Type] => param_error
[Id] => a3a12c22-1bc3-41ff-b92b-d923cd763e7f
[Date] => 1427279161
[errors] => stdClass Object
(
[AuthorId] => The AuthorId field is required.
)
)
)
)
[1] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/restTool.inc
[line] => 93
[function] => RunRequest
[class] => MangoPay\RestTool
[type] => ->
[args] => Array
(
)
)
[2] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/apiBase.inc
[line] => 157
[function] => Request
[class] => MangoPay\RestTool
[type] => ->
[args] => Array
(
[0] => /payins/card/web/
[1] => POST
[2] => Array
(
[CreditedWalletId] => 6183033
[CardType] => CB_VISA_MASTERCARD
[ReturnURL] => http://itsaboutmyafrica.com/checkout/order-received/1804?key=wc_order_551277ae9f920
[Culture] => TN
[SecureMode] => DEFAULT
[DebitedFunds] => Array
(
[Currency] => EUR
[Amount] => 46000
)
[Fees] => Array
(
[Currency] => EUR
[Amount] => 0
)
[Tag] => 1804
)
)
)
[3] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/mangoPaySDK/tools/apiPayIns.inc
[line] => 17
[function] => CreateObject
[class] => MangoPay\ApiBase
[type] => ->
[args] => Array
(
[0] => payins_card-web_create
[1] => MangoPay\PayIn Object
(
[CreditedWalletId] => 6183033
[PaymentType] => CARD
[PaymentDetails] => MangoPay\PayInPaymentDetailsCard Object
(
[CardType] => CB_VISA_MASTERCARD
[CardId] =>
)
[ExecutionType] => WEB
[ExecutionDetails] => MangoPay\PayInExecutionDetailsWeb Object
(
[RedirectURL] =>
[ReturnURL] => http://itsaboutmyafrica.com/checkout/order-received/1804?key=wc_order_551277ae9f920
[TemplateURL] =>
[Culture] => TN
[SecureMode] => DEFAULT
)
[AuthorId] =>
[CreditedUserId] =>
[DebitedFunds] => Array
(
[Currency] => EUR
[Amount] => 46000
)
[CreditedFunds] =>
[Fees] => Array
(
[Currency] => EUR
[Amount] => 0
)
[Status] =>
[ResultCode] =>
[ResultMessage] =>
[ExecutionDate] =>
[Type] =>
[Nature] =>
[Id] =>
[Tag] => 1804
[CreationDate] =>
)
[2] => \MangoPay\PayIn
)
)
[4] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/gateway/class-wm-fonctions-gateway.php
[line] => 129
[function] => Create
[class] => MangoPay\ApiPayIns
[type] => ->
[args] => Array
(
[0] => MangoPay\PayIn Object
(
[CreditedWalletId] => 6183033
[PaymentType] => CARD
[PaymentDetails] => MangoPay\PayInPaymentDetailsCard Object
(
[CardType] => CB_VISA_MASTERCARD
[CardId] =>
)
[ExecutionType] => WEB
[ExecutionDetails] => MangoPay\PayInExecutionDetailsWeb Object
(
[RedirectURL] =>
[ReturnURL] => http://itsaboutmyafrica.com/checkout/order-received/1804?key=wc_order_551277ae9f920
[TemplateURL] =>
[Culture] => TN
[SecureMode] => DEFAULT
)
[AuthorId] =>
[CreditedUserId] =>
[DebitedFunds] => Array
(
[Currency] => EUR
[Amount] => 46000
)
[CreditedFunds] =>
[Fees] => Array
(
[Currency] => EUR
[Amount] => 0
)
[Status] =>
[ResultCode] =>
[ResultMessage] =>
[ExecutionDate] =>
[Type] =>
[Nature] =>
[Id] =>
[Tag] => 1804
[CreationDate] =>
)
)
)
[5] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/wordpressmangopay/includes/gateway/class-wm-woocommerce-gateway.php
[line] => 152
[function] => wm_create_payin
[class] => WM_Fonction_Gateway
[type] => ::
[args] => Array
(
[0] =>
[1] => http://itsaboutmyafrica.com/checkout/order-received/1804?key=wc_order_551277ae9f920
[2] => 1804
)
)
[6] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/woocommerce/includes/class-wc-checkout.php
[line] => 646
[function] => process_payment
[class] => WM_Woocommerce_Gateway
[type] => ->
[args] => Array
(
[0] => 1804
)
)
[7] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-content/plugins/woocommerce/includes/class-wc-ajax.php
[line] => 369
[function] => process_checkout
[class] => WC_Checkout
[type] => ->
[args] => Array
(
)
)
[8] => Array
(
[function] => checkout
[class] => WC_AJAX
[type] => ::
[args] => Array
(
[0] =>
)
)
[9] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-includes/plugin.php
[line] => 496
[function] => call_user_func_array
[args] => Array
(
[0] => Array
(
[0] => WC_AJAX
[1] => checkout
)
[1] => Array
(
[0] =>
)
)
)
[10] => Array
(
[file] => /homepages/17/d555245609/htdocs/Itsaboutmyafrica/wp-admin/admin-ajax.php
[line] => 85
[function] => do_action
[args] => Array
(
[0] => wp_ajax_woocommerce_checkout
)
)
)
[previous:Exception:private] =>
[_code] => 400
)
I used mangopay wordpress plugin 1.0.5 with woocommerce 2.3 and ignitiondeck-crowdfunding
Could you please help me get rid from these errors?
Thanks in advance.
I was having this issue and it turns out that Address is missing. I added
$user->Address = null;
to solve this.
I am trying to cycle through a select list on an exposed form filter and hide terms I don't need.
With my select box being called ttd-tid I tried:
foreach ($form['ttd_tid']['#options'] as $tid => $term){
if (in_array($tid,$child_terms_tids)){
//leave it
}else{
//remove it
unset($form['ttd_tid']['#options'][$tid]);
}
}
But $tid is not the option value. It seems to be the count.
I tried
foreach ($form['ttd_tid']['#options']['option'] as $tid => $term){
But that didn't work either.
Essentially, I need to compare the value of the option to an array and hide it. And I can't seem to get it to work since $tid seems to be yet another array.
My $form array looks like this
Array ( [#info] => Array ( [filter-field_things_to_do_tags_tid] => Array ( [operator] => field_things_to_do_tags_tid_op [value] => ttd_tid [label] => Things to Do Tags [description] => ) ) [ttd_tid] => Array ( [#type] => select [#options] => Array ( [All] => - Any - [0] => stdClass Object ( [option] => Array ( [86] => Amusement Parks ) ) [1] => stdClass Object ( [option] => Array ( [63] => -Theme Parks ) ) [2] => stdClass Object ( [option] => Array ( [611] => -Water Parks ) ) [3] => stdClass Object ( [option] => Array ( [612] => Arts, Culture & History ) ) [4] => stdClass Object ( [option] => Array ( [1] => -Architecture & Landmarks ) ) [5] => stdClass Object ( [option] => Array ( [30] => -Galleries ) ) [6] => stdClass Object ( [option] => Array ( [32] => -History & Culture ) ) [7] => stdClass Object ( [option] => Array ( [83] => -Museums ) ) [8] => , etc, etc
Try
foreach ($form['ttd_tid']['und']['#options'] as $tid => $term){
If that dont work using the following within you form alter it will print out the array to /admin/reports/dblog when you load the page
watchdog('$form', '<pre>'.print_r($form, true).'</pre>');
The reason I need to do this is because we are turning our CMS over to a client and we need them to follow the rules but we can't just change the value outright as there is content in the system that breaks the max length rules for some fields and we don't want them to get chopped. So going ahead we just want to target all new content by targeting the new content form (node-form). I currently have a hook that successfully changes the max value for the title below:
function myForm_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] == 'node-form') {
$form['title']['#maxlength'] = 30;
}
}
I need to add one for a field named "field_feature_desc". I printed the form info to the screen and here is the info I received for that field:
[field_feature_desc] => Array
(
[field_name] => field_feature_desc
[type_name] => article
[display_settings] => Array
(
[weight] => 11
[parent] =>
[label] => Array
(
[format] => above
)
[teaser] => Array
(
[format] => default
[exclude] => 1
)
[full] => Array
(
[format] => default
[exclude] => 1
)
[4] => Array
(
[format] => default
[exclude] => 1
)
[2] => Array
(
[format] => default
[exclude] => 0
)
[3] => Array
(
[format] => default
[exclude] => 0
)
)
[widget_active] => 1
[type] => text
[required] => 0
[multiple] => 0
[db_storage] => 0
[module] => text
[active] => 1
[locked] => 0
[columns] => Array
(
[value] => Array
(
[type] => text
[size] => big
[not null] =>
[sortable] => 1
[views] => 1
)
)
[text_processing] => 0
[max_length] =>
[allowed_values] =>
[allowed_values_php] =>
[widget] => Array
(
[rows] => 5
[size] => 60
[default_value] => Array
(
[0] => Array
(
[value] =>
[_error_element] => default_value_widget][field_feature_desc][0][value
)
)
[default_value_php] =>
[label] => Feature Description
[weight] => -3
[description] => Description text in the feature area on the homepage, if applicable
[type] => text_textfield
[module] => text
)
)
If you have an idea how i would code for this additional field it would be much appreciated. I've been trying the following:
$form['field_feature_desc']['max_length'] = 70;
$form['field_feature_desc']['columns']['value']['length'] = 70;
Sorry if there is already an answer for this question, I looked all over for one and couldn't locate one.
I discovered an array outside of the one i was working with. So the code that did it was:
$form['#field_info'] = array(
'field_feature_desc' => array(
'max_length' => 70,
),
);
Or a shorter version is:
$form['#field_info']['field_feature_desc']['max_length'] = 70;