Skip to main content
Share Your Experience: Take the 2024 Developer Survey
19 votes
Accepted

Too slow when using both 'tax_query' and 'meta_query' both in WP_Query

This is a stupendously expensive query, you can mitigate but you can't eliminate the performance issues. So lets start with the low hanging fruit and work our way up to the big problems ...
Tom J Nowell's user avatar
  • 59.5k
13 votes

getting attachement images src and add classes

If you only want to add an extra class, then you should use wp_get_attachment_image. It has few extra params, and the last one is used for setting class names. Sample usage: <?php echo ...
Ionut Staicu's user avatar
8 votes

How to extract data from a post meta serialized array?

You can use this WordPress codex which converts into an array. maybe_unserialize($data); https://developer.wordpress.org/reference/functions/maybe_unserialize/
Dhara Talaviya's user avatar
6 votes
Accepted

Replace text inside a huge multidimensional array

Try this php built-in function array_walk_recursive function wpse_do_something_on_data() { $data = array( 'repeater-1' => array( array( 'user_defined_field1' ...
nonsensecreativity's user avatar
6 votes

Sorting list of sites from multisite network using wp_get_sites

@birgire get_sites() example is the correct way. I would have just added this as a comment, but don't have enough dang reputation! The only change, is that public accepts an integer, not a bool. See ...
fostertime's user avatar
6 votes
Accepted

Check if term object is in array

WordPress has the wp_list_pluck function, which can be helpful here. We can make an array of just term IDs from the array of objects like: $term_ids = wp_list_pluck( $subcat_terms, 'term_id' ); Then ...
Milo's user avatar
  • 78.5k
6 votes
Accepted

How to update serialized post meta?

You must read custom field themeOps first, update index option3 in array and then save/update whole array. $kisiArray = get_post_meta( $post_id, 'themeOps' ); $kisiArray['option3'] = 'peach'; ...
nmr's user avatar
  • 4,500
6 votes

Getting an array out of WPQuery

If you only want IDs, the query will consume much less memory if you use the fields parameter to just get that one field back in an array: function ids(){ $args = array( 'numberposts' =&...
Milo's user avatar
  • 78.5k
5 votes

Create an array with a string key from wpdb->get_results

Found the solution: $currentProducts = $wpdb->get_results("SELECT feedid, id, size, price FROM products WHERE shopid = $shopid", OBJECT_K); The OBJECT_K parameter makes an associative array with ...
Louisa's user avatar
  • 205
5 votes

update_post_meta from data in multidimensional array created from a form

First off, there's a problem with your array. Arrays can not have duplicate keys. So, only the first key will be saved. You need to change your form to something like this. <input name='...
Abhik's user avatar
  • 2,911
5 votes
Accepted

Looping through WP_Post Object

Based on what you said about a post being completed, you can check if its status is completed and if it is, then point to the title: foreach ( $lessons as $lesson ){ if( $lesson['status'] == '...
Johansson's user avatar
  • 15.3k
5 votes
Accepted

Get Term names from WP Term Object

Here's an alternative using the handy wp_list_pluck(): $terms = get_terms(array( 'taxonomy' => 'category', 'hide_empty' => false, )); $slugs = wp_list_pluck( $terms, 'slug' ); $...
birgire's user avatar
  • 67.4k
5 votes
Accepted

How can I pick a single post from the latest 3?

Here's my approach... First you have to select 3 latest posts, then you have to pick random one of them... But it's easier to shuffle selected posts than picking only one of them - that way you can ...
Krzysiek Dróżdż's user avatar
4 votes

WP Cron Doesn't Execute When Time Elapses

For anyone protecting their (development) site from public access, HTTP Authentication can be the cause of WP Cron not functioning. In case it can help anyone, here is my list of things I did before ...
sampi's user avatar
  • 159
4 votes

How to use an array of categories as a dropdown?

In addtion to the answer, if you want that you dropdown value has the id of the category you should do something like this: $categories_array = array(); $categories = get_terms('tax_id', array('...
laviku's user avatar
  • 143
4 votes
Accepted

Saving an array in a single custom field

You can json_encode() it which will make it a string that you can put into a custom field then just ensure you json_decode() it to bring it back to an object or json_decode($data, true) to bring it ...
Ray's user avatar
  • 230
4 votes

Problem with displaying HTML content after in_array

Notice that there are no echo statements, so nothing will be output. You need to echo the result, or nothing will be sent to the browser. If it was just a basic loop (without a blue container) I ...
Tom J Nowell's user avatar
  • 59.5k
4 votes
Accepted

How to insert new element to existing array in usermeta?

Let's analyze your code and what it does. Assuming that the current fruits stored in the database are 'pineapple' and 'orange', get_user_meta(2, 'fruits', false) will return something like this: ...
swissspidy's user avatar
  • 3,844
4 votes
Accepted

WordPress i18n in Array throws Error

This is a PHP issue, not specific to WordPress. PHP does not allow you to declare a property as an expression. It should be a literal. So you can not call a function inside a property declararion. (...
Leo Germani's user avatar
4 votes
Accepted

how to get post order by post id wp_query?

OK, so you want to define posts order by yourself. WP_Query allows you to do that - you'll have to use orderby => post__in to achieve it. And that's what you do. So why isn't it working? Because of a ...
Krzysiek Dróżdż's user avatar
4 votes
Accepted

Gutenberg: How filter blocks of a certain type with parse_blocks recursively?

parse_blocks() simply returns all blocks found in the post content, so for what you're trying to get, you could use a function which calls itself recursively like so: function my_find_heading_blocks( $...
Sally CJ's user avatar
  • 39.3k
3 votes
Accepted

Sorting list of sites from multisite network using wp_get_sites

Using get_sites() in WP 4.6+ It looks like wp_get_sites() will be deprecated in WP 4.6. The new replacement is: function get_sites( $args = array() ) { $query = new WP_Site_Query(); ...
birgire's user avatar
  • 67.4k
3 votes

How to fetch an array in $wpdb?

Going to take a wild stab, but ARRAY_A will return an associative array You are then trying to access this associative array as an object Simple change: foreach($results as $result){ echo $...
Jamie's user avatar
  • 71
3 votes
Accepted

Building a scalable Wordpress favouriting plugin - one serialised meta value array or many meta records

You forgot option 3 - Add a special table in which the pair (user,post id) will be the index. Ok I am not a MySQL person so maybe it is too extreme, but maybe having two tables one with users as index ...
Mark Kaplun's user avatar
  • 23.4k
3 votes

Get posts by list of post IDs ordered by those IDs?

To Maintain the Order as by Given IDs Since 3.5 we have option called orderby => post__in Preserve post ID order given in the post__in array (available since Version 3.5). $ordered_posts = new ...
Muhammad Asadullah's user avatar
3 votes

Get list of all registered post types slugs

The easiest way is the following using WordPress function get_post_types(); <?php $get_cpt_args = array( 'public' => true, '_builtin' => false ); $post_types = get_post_types( $...
Aamer Shahzad's user avatar
3 votes

Sorting list of sites from multisite network using wp_get_sites

I wasn't able to get get_sites working, so here is a solution using wp_get_sites until further documentation is released on the new version. wp_get_sites returns a lot of data about the subsite, ...
Morgan's user avatar
  • 489
3 votes

Sorting list of sites from multisite network using wp_get_sites

Here is my code for running through sites in WP 4.6 and sites using WP < 4.6. If you are producing themes or plugins to be used by the general public you have no control over which version of ...
Lance Cleveland's user avatar
3 votes
Accepted

WP Job Manager Category Drop-down; Change Placeholder Text Via Filter

Based on the testing that I did, the placeholder string Choose a category… cannot be filtered with the submit_job_form_fields filter, but there are still ways to change that string. One way to alter ...
Dave Romsey's user avatar
  • 17.7k
3 votes

Need to get specific data from array

I didn't test anything and read your code, but it seems that you can get_permalink() as I was told in the comment but it's true that you will get the attachment page not its url. You can access to ...
Benoti's user avatar
  • 2,436

Only top scored, non community-wiki answers of a minimum length are eligible