Skip to main content
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
  • 60k
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

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.7k
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,521
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.7k
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,921
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.6k
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
  • 60k
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,894
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

How to query serialized array by comparing greater than (>=)?

serialised array in a meta_value for query It's not a good idea to save serialised array in a meta_value if you plan to query with any value within that serialized array later. So, the best option is ...
Fayaz's user avatar
  • 8,957
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.7k
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.8k
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

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,486
3 votes
Accepted

get_tags() return an empty array after added tags with wp_insert_term()

Thanks to @Milo I could fix it. The problem was, that the tags were not assigned to any post. display.php $tags = get_tags(array('hide_empty' => false)); The hide_empty param do the trick.
sydev's user avatar
  • 123
3 votes
Accepted

Using a javascript file to access a get posts array

Yep, you're looking for wp_localize_script(). Do away with the global. add_action( 'wp_enqueue_scripts', 'wpse_enqueue_scripts' ); function wpse_enqueue_scripts() { wp_enqueue_script( 'wpse-main',...
brianjohnhanna's user avatar
3 votes
Accepted

Post meta as array looks like string instead of array

Your data will be serialized, if you save non-scalar values (e.g. array, object) as post meta. You must unserialize it: $subs = get_post_meta( $post->ID ); $subs['_q1_subs'][0] = unserialize($subs[...
ahendwh2's user avatar
  • 696
3 votes

How am I able to get the value out of cookie array when I push a button?

You need to put the id of each item inside form to indicate the item that will be deleted. <?php $all_favorites= unserialize($_COOKIE['favorites']); echo '<table>'; foreach($all_favorites ...
Laxmana's user avatar
  • 1,332
3 votes
Accepted

Use Transient API to cache queries for all posts in all categories?

You're saving every query object for each category to the same transient. Because this happens fast and time frame is one day, you're always getting the query object for the first category back. Make ...
Nicolai Grossherr's user avatar

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