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 props.attributes.cards.push({         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:

props.attributes.cards.push({     name: 'Enter name.',     title: 'Enter title.' })  return props.setAttributes( {     cards: props.attributes.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:

a:13:{s:7:"relaxed";a:7:{s:4:"name";s:7:"Relaxed";s:5:"votes";i:377;s:7:"term_id";s:5:"28595";s:4:"slug";s:7:"relaxed";s:4:"rank";i:1;s:6:"status";b:1;s:4:"icon";s:70:"https://example.com/wp-content/uploads/2020/12/Relaxed.svg";}s:5:"happy";a:7:{s:4:"name";s:5:"Happy";s:5:"votes";i:293;s:7:"term_id";s:5:"28596";s:4:"slug";s:5:"happy";s:4:"rank";i:2;s:6:"status";b:1;s:4:"icon";s:68:"https://example.com/wp-content/uploads/2020/12/Happy.svg";}s:8:"euphoric";a:7:{s:4:"name";s:8:"Euphoric";s:5:"votes";i:267;s:7:"term_id";s:5:"28597";s:4:"slug";s:8:"euphoric";s:4:"rank";i:3;s:6:"status";b:1;s:4:"icon";s:71:"https://example.com/wp-content/uploads/2020/12/Euphoric.svg";}s:8:"uplifted";a:7:{s:4:"name";s:8:"Uplifted";s:5:"votes";i:191;s:7:"term_id";s:5:"28598";s:4:"slug";s:8:"uplifted";s:4:"rank";i:4;s:6:"status";b:1;s:4:"icon";s:71:"https://example.com/wp-content/uploads/2020/12/Uplifted.svg";}s:6:"sleepy";a:7:{s:4:"name";s:6:"Sleepy";s:5:"votes";i:181;s:7:"term_id";s:5:"28599";s:4:"slug";s:6:"sleepy";s:4:"rank";i:5;s:6:"status";b:1;s:4:"icon";s:69:"https://example.com/wp-content/uploads/2020/12/Sleepy.svg";}s:6:"hungry";a:7:{s:4:"name";s:6:"Hungry";s:5:"votes";i:178;s:7:"term_id";s:5:"28600";s:4:"slug";s:6:"hungry";s:4:"rank";i:6;s:6:"status";b:1;s:4:"icon";s:69:"https://example.com/wp-content/uploads/2021/01/Hungry.svg";}s:6:"giggly";a:7:{s:4:"name";s:6:"Giggly";s:5:"votes";i:122;s:7:"term_id";s:5:"28602";s:4:"slug";s:6:"giggly";s:4:"rank";i:7;s:6:"status";b:1;s:4:"icon";s:69:"https://example.com/wp-content/uploads/2021/01/Giggly.svg";}s:8:"creative";a:7:{s:4:"name";s:8:"Creative";s:5:"votes";i:109;s:7:"term_id";s:5:"28601";s:4:"slug";s:8:"creative";s:4:"rank";i:8;s:6:"status";b:1;s:4:"icon";s:71:"https://example.com/wp-content/uploads/2021/01/Creative.svg";}s:6:"tingly";a:7:{s:4:"name";s:6:"Tingly";s:5:"votes";i:107;s:7:"term_id";s:5:"28605";s:4:"slug";s:6:"tingly";s:4:"rank";i:9;s:6:"status";b:1;s:4:"icon";s:69:"https://example.com/wp-content/uploads/2021/01/Tingly.svg";}s:7:"focused";a:7:{s:4:"name";s:7:"Focused";s:5:"votes";i:102;s:7:"term_id";s:5:"28603";s:4:"slug";s:7:"focused";s:4:"rank";i:10;s:6:"status";b:1;s:4:"icon";s:70:"https://example.com/wp-content/uploads/2021/01/focused.svg";}s:9:"talkative";a:7:{s:4:"name";s:9:"Talkative";s:5:"votes";i:99;s:7:"term_id";s:5:"28606";s:4:"slug";s:9:"talkative";s:4:"rank";i:11;s:6:"status";b:1;s:4:"icon";s:72:"https://example.com/wp-content/uploads/2021/01/Talkative.svg";}s:7:"aroused";a:7:{s:4:"name";s:7:"Aroused";s:5:"votes";i:74;s:7:"term_id";s:5:"28607";s:4:"slug";s:7:"aroused";s:4:"rank";i:12;s:6:"status";b:1;s:4:"icon";s:70:"https://example.com/wp-content/uploads/2021/01/Aroused.svg";}s:9:"energetic";a:7:{s:4:"name";s:9:"Energetic";s:5:"votes";i:65;s:7:"term_id";s:5:"28604";s:4:"slug";s:9:"energetic";s:4:"rank";i:13;s:6:"status";b:1;s:4:"icon";s:72:"https://example.com/wp-content/uploads/2021/01/Energetic.svg";}} 

And data in array format:

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

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?