Change Index Key to Name - php

I have built a multidimensional array for usage in a custom Wordpress query. Although it is Wordpress I feel that this is more a basic PHP question.
The array that I have now outputs everything properly, accept for the index value. I need it to be a string instead of an integer.
Here is what I need my ouput to be
Array (
[post_type] => property
[posts_per_page] => -1
[tax_query] => Array (
[0] => Array (
[taxonomy] => state
[field] => slug
[terms] => illinois
)
[1] => Array (
[taxonomy] => illinois_county
[field] => slug
[terms] => fulton
)
)
)
And here is what is actually being output
Array (
[post_type] => property
[posts_per_page] => -1
[0] => Array (
[0] => Array (
[taxonomy] => state
[field] => slug
[terms] => illinois
)
[1] => Array (
[taxonomy] => illinois_county
[field] => slug
[terms] => fulton
)
)
)
The only difference being the key of the second array which is 0 in mine and needs to be tax_query.
To get this Array I am declaring $tax_query_array = array(); and then dropping my child arrays as needed depending on what variables are present in the url, such as $tax_query_array[] = $state_array; and $tax_query_array[] = $county_array;. Then finally calling $tax_query_array where i need the final multidimensional array output.
Only thing stopping me is the initial [0] which needs to instead be [tax_query].
Here is the full code:
$tax_query_array = array();
$tax_query_array['tax_query'][] = $state_array;
$tax_query_array['tax_query'][] = $county_array;
$tax_query_array['tax_query'][] = $price_range_array;
$taxonomy_args = array(
'post_type' => 'property',
'posts_per_page' => -1,
$tax_query_array
}
Output from changing $tax_query_array[] = $county_array; to $tax_query_array['tax_query'][] = $county_array; via MikeBrant:
Array (
[post_type] => property
[posts_per_page] => -1
[0] => Array (
[tax_query] => Array (
[0] => Array (
[taxonomy] => state
[field] => slug
[terms] => illinois
)
[1] => Array (
[taxonomy] => illinois_county
[field] => slug
[terms] => fulton
)
)
)
)

Try this code:
$taxonomy_args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'tax_query'=> array($state_array, $county_array, $price_range_array)
}

Related

WordPress - Order by ACF date, date first, then empty fields last

I'm trying to order content by the dick picker provided by ACF.
order by date field
Dates in order of soonest(past/present) to farthest out (ASC ordering).
Then any post that meets the criteria but doesn't have a date set would be last.
I have the following Query arguments:
(
[post_type] => people
[posts_per_page] => 10
[paged] => 1
[meta_query] => Array
(
[0] => Array
(
[key] => has_scheduling
[value] => 1
)
[1] => Array
(
[key] => custom_ID
[compare] => !=
[value] =>
)
[2] => Array
(
[key] => date_picker
[compare] => !=
[value] =>
)
)
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => people_types
[field] => term_id
[terms] => 153
)
)
[meta_key] => date_picker
[orderby] => meta_value_num
[order] => ASC
)
I noticed that this Query will only load the first part correctly. It grabs content based on all the parameters, then orders is by date, but it excludes the content that doesn't have a date set.
I've tried this as well:
(
[post_type] => people
[posts_per_page] => 10
[paged] => 1
[meta_query] => Array
(
[0] => Array
(
[key] => has_scheduling
[value] => 1
)
[1] => Array
(
[key] => custom_ID
[compare] => !=
[value] =>
)
[2] => Array
(
[relation] => OR
[0] => Array
(
[key] => date_picker
[compare] => EXISTS
)
[1] => Array
(
[key] => date_picker
[compare] => NOT EXISTS
)
)
)
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => people_types
[field] => term_id
[terms] => 153
)
)
[orderby] => meta_value
[order] => ASC
)
So here I don't exclude empty fields, I check if it exists and then doesn't. Then order by the data again. I also switched to use meta_value instead of meta_value_num
Any tips or tricks, or if you have run into issue as well, I'd love to see what you did.
For anyone also trying to do the same, the end result for this ended using a custom a orderby filter. Example code below.
// Order By custom date.
add_filter('posts_orderby', function (string $orderby, WP_Query $query) {
if ($query->get('orderby') === '__custom_date__') {
$orderby = "mta.meta_value is NULL, mta.meta_value ='', mta.meta_value ASC";
}
return $orderby;
}, 10, 2);
// Extra join to order by custom date.
add_filter('posts_join', function (string $join, WP_Query $query) {
if ($query->get('orderby') === '__custom_date__') {
$join .= " LEFT JOIN wp_postmeta AS mta ON ( wp_posts.ID = mta.post_id AND mta.meta_key = 'date_picker')";
}
return $join;
}, 10, 2);
This is then used as a parameter that gets passed to orderby
$args['orderby'] = '__custom_date__';

Sorting in Elastic Search by defined type

I have this request to elastic service.
Url callback is:
GET
/es-main/Project,ProjectStage,Task,TaskComment,TicketComment,Client,User,Attachment/_search
How to add Sort improvement requirements that the results were in sequence as they are embedded in the URL + score? Now alphabetically by type. Example first is type Project, second is ProjectStage, etc...
Thanks all for answers...
GET /es-main/Project,ProjectStage,Task,TaskComment,TicketComment,Client,User,Attachment/_search
Array
(
[query] => Array
(
[query_string] => Array
(
[query] => *a*
[analyzer] => hunspell_cs
[fields] => Array
(
[0] => name
[1] => description
[2] => tags.name
[3] => text
[4] => email
[5] => filename
)
)
)
[from] => 0
[size] => 10
[sort] => Array
(
[_type] => ASC
[_score] => DESC
)
[aggs] => Array
(
[CountByType] => Array
(
[terms] => Array
(
[field] => _type
)
)
)
)
Okey, i have added in document _is_Project = 1, _is_ProjectStage = ,_is_Task = 1, etc...
and set ordering to:
order: {
'_is_Project': {'unmapped_type': 'long'}, // first
'_is_ProjectStage': {'unmapped_type': 'long'}, // second
'_is_Task': {'unmapped_type': 'long'}, // third - etc...
'_score': 'DESC'
}

How to differentiate a [key] and [value] from another [key] and [value] by its parent array [key]?

I hope this is the best way to ask this. Not sure how to word it. I was thinking there is a native PHP function to determine this, which leads me to think that maybe my wording in searches isn't the best.
I want to search my array for a specific [key] => [value].
If that [key] => [value] is found within my array I want to get another [key] => [value] from its array parent.
Examples from my code below to explain.
Example 1:
If [post_type] = [page] I want to get [activate_layout] = [value] from array [0].
Example 2:
If [post_type] = [post] I want to get [activate_layout] = [value] from array [1].
General Concept Example 3:
If [post_type] = [x] I want to get [activate_layout] = [x] from its parent array [x].
My question is how can I differentiate a [key] and [value] from another [key] and [value] by its parent array [key]?
Below is how my array data is stored.
[post_type_layouts] => Array
(
[0] => Array
(
[context] => Array
(
[option] => Array
(
[activate_layout] => 1
[post_type] => page
)
)
)
[1] => Array
(
[context] => Array
(
[option] => Array
(
[activate_layout] => 1
[post_type] => post
)
)
)
)
If I really understand your problem I think that the solution is more simple that you're doing.
I follow this array like example:
$arrayTest = [
0 => [
'context' => [
'option' => [
'post_type' => 'page',
],
],
],
1 => [
'context' => [
'option' => [
'post_type' => 'post',
],
],
],
];
And, surfing in that and to get the parent value of post_type I only create a foreach steatment where I use switch steatment to check the post_type value. It's something like this:
foreach ($arrayTest as $subLevel1) {
switch ($subLevel1['context']['option']['post_type']) {
case 'page':
$subLevel1['context']['option']['active_layout'] = 0;
break;
default:
$subLevel1['context']['option']['active_layout'] = 1;
break;
}
print_r($subLevel1);
}
My return is like your sample above, and I think that this solve your problem:
php -f testArray.php
Array
(
[context] => Array
(
[option] => Array
(
[post_type] => page
[active_layout] => 0
)
)
)
Array
(
[context] => Array
(
[option] => Array
(
[post_type] => post
[active_layout] => 1
)
)
)
Good code!

get array name from multilevel array in php

how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].

mongodb $inc a field inside an array

hi i have the follwing array structure
Array
(
[_id] => MongoId Object
(
[$id] => 538978ce8ead0ec1048b456c
)
[cartId] => 98374319ff71dbc3a84b842b7a443cf7
[products] => Array
(
[0] => Array
(
[productId] => 100343
[quantity] => 17
[name] => a
)
[1] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[2] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
And i'm having problems to increment the quantity of the products based on the productId
I now use the position but i have no reference on id
$oCartsCollection->update(array('cartId'=>'98374319ff71dbc3a84b842b7a443cf7'), array('$inc' => array('products.0.quantity'=>1)));
What you need to do is add to your query to select and element from your array and then use the positional $ operator in order to match that position:
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products.productId' => 100343
),
array('$inc' => array('products.$.quantity'=>1)));
The "dot" notation method is fine for accessing the productId element in this case. For multiple fields to match use $elemMatch instead
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products' => array(
'$elemMatch' => array(
'productId' => 100343,
'name' => 'a'
)
)
),
array('$inc' => array('products.$.quantity'=>1)));

Categories