How to filter and get specific keys from JSON objects array

I have a JSONB field containing a value in this structure:

[     {         "type": "small",         "name": "Josh",         "greeting": "Hello buddy!",         "hobby": "billiards",         "place": "Barcelona"     },     {         "type": "big",         "name": "Aziz",         "greeting": "Hey man!",         "hobby": "tennis",         "place": "Montpellier"     } ] 

I want to include this output if type is equal to "big":

{     "greeting": "Hey man! How're you doing Aziz?",     "place": "Montpellier" } 
  1. In the first row, I combined two values into one with some additional words.
  2. I don’t want my whole output to be empty if there isn’t type value equal to "big". Instead, output values of type value equal to "small" as a fallback.
  3. If there is not a fallback with type equal to "small", then output an empty string or NULL value but without Nulling the whole query result. Or preferably, not include these keys if no value is present.

I know I can combine values into one by using CONCAT but don’t know how to build this query and integrate it. I can’t loop through the array and output the values of the object that matches my filter. Tried CASE but still wasn’t able to get the result I want.

I use PostgreSQL 13.

Build array and compare two foreach and wp_update_post on non-matching IDs

So I’m somewhat confused and need some assistance from the community, so I have a piece of code that I’m working on and I would like to compare two arrays and then if IDs don’t match, to put the post in pending state.

I have the following method:

/**  * Update Office titles  */ function update_office_titles() {     $  office_data = build_offices_array();     $  compare_ids = [];     foreach ($  office_data as $  office) {         $  compare_ids[] = [             'wp_office_id' => $  office['office_id']         ];     }     foreach (api_offices_raw() as $  api_office_id) {         $  compare_ids[] = [             'api_id' => $  api_office_id->id         ];     }      print_r($  compare_ids);      echo 'All of the office titles have been updated'; } 

Here, I’m grabbing all the office_id post_meta and getting all the post_meta values in the DB:

foreach ($  office_data as $  office) {     $  compare_ids[] = [         'wp_office_id' => $  office['office_id']     ]; } 

I get the following return:

[243] => Array     (         [wp_office_id] => 3     )  [244] => Array     (         [wp_office_id] => 4     ) 

Then I have this foreach that loops through an API with IDs:

foreach (api_offices_raw() as $  api_office_id) {     $  compare_ids[] = [         'api_id' => $  api_office_id->id     ]; } 

I get this output:

[246] => Array     (         [api_id] => 1     )  [247] => Array     (         [api_id] => 3     ) 

Here is what I was attempting to achieve:

Check api_id‘s and match them to the wp_office_id key values, if there are matching IDs, don’t do nothing, otherwise call a wp_update_post and set the status to pending on $ office['post_id'] on all IDs from wp_office_id that don’t match.

All help is appreciated!

Update UI when pushing new object to an array attribute

In one of my blocks I’m using an array of cards so that a user can add multiple cards to a block without having to use nested blocks and complicate the HTML.

The attributes look like this:

attributes: {     cards: {         type: 'array',         source: 'query',         selector: '.staff-list-item',         default: [],         query: {             name: {                 type: 'array',                 source: 'children',                 selector: 'h3',                 default: 'Enter name.',             },             title: {                 type: 'array',                 source: 'children',                 selector: 'p',                 default: 'Enter title.',             },             mediaID: {                 type: 'number',                 source: 'attribute',                 attribute: 'data-id',                 selector: 'img',             },             mediaURL: {                 type: 'string',                 source: 'attribute',                 attribute: 'src',                 selector: 'img',             },         },     }, } 

I then have a button component that can add one of these cards:

el(components.Button, { className: 'button button-large', onClick: onAddCard }, 'Add Card') 

And the function it calls is:

function onAddCard() {     return{         name: 'Enter name.',         title: 'Enter title.'     }) } 

So I basically push a new object into the cards… however what I’ve found is that even though I’m returning the objects… it doesn’t update the UI until I click OUT of the block… how can I get it to work similar to setAttributes?

I even tried:{     name: 'Enter name.',     title: 'Enter title.' })  return props.setAttributes( {     cards: }); 

How to Fix Undefined array key “action” on WordPress site running on Php8

I’m getting error PHP message: PHP Warning: Undefined array key "action" for this code:

function disable_lost_password() {     if ( in_array( $  _GET['action'], array('lostpassword', 'retrievepassword') ) ) {         wp_redirect( wp_login_url(), 301 );         exit;     } } add_action( "init", "disable_lost_password" ); 

How to fix it?

how to get users with usermeta include array of an array

i have a user_meta for users that them is array like below:

user_saved_posts = [31289,31482,27641] 

and i want to get users that their user_meta include an item of an array like below:

goal_posts = [31289,31422,77641,41289,21482,17641] 

if user have an item of goal_posts array must returned them. i use below code but this code worked if i have a value for search in user_meta

$  args = [         'meta_query' => [             [                 [                     'key' => 'saved_posts',                     'value' => sprintf(':"%s";', 31289),                     'compare' => 'LIKE'                 ]             ]         ]     ];     get_users($  args); 

Postgresql: sort by value position in array column, then by secondary order

I’m not quite sure what the best way to phrase this is…

So in my DB there is pillars text array which is basically an enum where providers ordered what values meant the most to their business, from most important to providing that value for their clients, to least important.

I’m using PostGIS to query providers in a specific area, and want to return providers ordered first by the pillar that a client selected they were looking for, then by closest location.

so if the pillars all have values ['a', 'b', 'c', 'd'], in any order depending on what providers selected, and the client selected pillar c

the results of the query would preferably return any/all providers that have pillar c at array index 0 first, ordered by distance to geopoint, then by providers that have pillar c at array index 1 second ordered by distance to client geopoint, then idx 2, then idx 3

I’m really only looking for the top 3 results in all cases, and providers with pillar c at idx 1 would only be needed if there were less than 3 results for index 0

Is this possible to pull off in a single query? or should I just run it with a where clause and check the results length until I have 3 results?

The pillars column is indexed with a gin index btw

WordPress meta query for serialize associated array meta value

Serialize array value is:


And data in array format:

Array (     [relaxed] => Array         (             [name] => Relaxed             [votes] => 377             [term_id] => 28595             [slug] => relaxed             [rank] => 1             [status] => 1             [icon] =>         )      [happy] => Array         (             [name] => Happy             [votes] => 293             [term_id] => 28596             [slug] => happy             [rank] => 2             [status] => 1             [icon] =>         )      [euphoric] => Array         (             [name] => Euphoric             [votes] => 267             [term_id] => 28597             [slug] => euphoric             [rank] => 3             [status] => 1             [icon] =>         )      [uplifted] => Array         (             [name] => Uplifted             [votes] => 191             [term_id] => 28598             [slug] => uplifted             [rank] => 4             [status] => 1             [icon] =>         )      [sleepy] => Array         (             [name] => Sleepy             [votes] => 181             [term_id] => 28599             [slug] => sleepy             [rank] => 5             [status] => 1             [icon] =>         )      [hungry] => Array         (             [name] => Hungry             [votes] => 178             [term_id] => 28600             [slug] => hungry             [rank] => 6             [status] => 1             [icon] =>         )      [giggly] => Array         (             [name] => Giggly             [votes] => 122             [term_id] => 28602             [slug] => giggly             [rank] => 7             [status] => 1             [icon] =>         )      [creative] => Array         (             [name] => Creative             [votes] => 109             [term_id] => 28601             [slug] => creative             [rank] => 8             [status] => 1             [icon] =>         )      [tingly] => Array         (             [name] => Tingly             [votes] => 107             [term_id] => 28605             [slug] => tingly             [rank] => 9             [status] => 1             [icon] =>         )      [focused] => Array         (             [name] => Focused             [votes] => 102             [term_id] => 28603             [slug] => focused             [rank] => 10             [status] => 1             [icon] =>         )      [talkative] => Array         (             [name] => Talkative             [votes] => 99             [term_id] => 28606             [slug] => talkative             [rank] => 11             [status] => 1             [icon] =>         )      [aroused] => Array         (             [name] => Aroused             [votes] => 74             [term_id] => 28607             [slug] => aroused             [rank] => 12             [status] => 1             [icon] =>         )      [energetic] => Array         (             [name] => Energetic             [votes] => 65             [term_id] => 28604             [slug] => energetic             [rank] => 13             [status] => 1             [icon] =>         )  ) 

I want to match the slug and get rank between 1 to 5 only. Let me know if there any solution for this.

Do Continuous effects in an array remain after you switch to a different power?

Note: This is different to What happens when you use a continuous power in an array and then switch to a different power? as that covers 2nd edition, and I’m focused on 3rd.

Say you have an array with two powers: one is a Create effect with Increased Duration (Continuous) extra, the other is an attack of some kind. You use the former to create a box, and then switch to the other. Does the box remain, or does it vanish?

The rules are ambiguous on this point, and there is a strong argument for either position from both a rules and a thematic perspective

The description for Alternate Effect states:

Alternate Effects cannot be used or maintained at the same time as other Alternates in the same array; they are mutually exclusive

The description for the Continuous duration states:

The effect lasts as long as you wish, without any action required on your part. Once active, it stays that way until you choose to deactivate it

From a rules perspective, one could argue that since one is not maintaining the effect, one would be permitted to swap between powers without dismissing the effect. On the other hand, one could argue that the effect being in place at all counts as the power being ‘used’.

From a thematic perspective, one could argue for effects remaining with the following example: Imagine a superhero, let’s call her Sue. Sue has the power to fire Ice Beams, with two applications: as an attack, or to build ice constructs. Since both rely on her firing Ice Beams, and she can only use the beams for one at a time, it makes sense that they would be an array. In addition, once her constructs are created, she doesn’t need to sustain them (ignoring ice melting for this example). As such, it would make sense for the constructs to be Continuous. If she were to build a construct one round, then attack with her beams the next, it would make little sense for the construct to suddenly disappear, as they were not reliant on her in any way. However, one could argue that the two effects being able to be applicable at the same time violates the concept of an array.

I’ve checked the forums, and there is a solid 3 pages of discussion there with no consensus, so I’m looking for an official ruling on this if there is one at all. Thanks in advance.

How to get a value-only flat array from $wpdb->get_results when selecting a single column, without foreach()?

My query is:

$  var = $  wpdb->get_results("SELECT field FROM {$  wpdb->prefix}table", ARRAY_A); var_dump($  var); 

it returns someting like array(2) { [0]=> array(1) { ["field"]=> string(5) "test1" } [1]=> array(1) { ["field"]=> string(t) "test2" }. I.e. each item is a row with a single name-value pair.

What I want is array(2) { [0]=> string(5) "test1" [1]=> string(5) "test2" }

Currently I achieve it like this:

$  var = $  wpdb->get_results("SELECT field FROM {$  wpdb->prefix}table"); foreach($  var as $  v_key => $  v_val) $  var[$  v_key] = $  v_val['field']; var_dump($  var); 

Is there a shorter way to do this?