2022 Developer Survey is open! Take survey.

Hot answers tagged

16 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 ...
user avatar
  • 51.8k
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 ...
user avatar
13 votes
Accepted

How to store the_title() into a variable to reutrn the value, not just echo it

You can use get_the_title() to return the current post title in the loop.
user avatar
  • 3,358
9 votes
Accepted

When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?

When multiple meta values are used for same meta key, WordPress store the values in different database rows. So every array, is stored in one row. Initial Data Let's assume you have an array like ...
user avatar
  • 44.6k
6 votes
Accepted

How can I get an array of all IDs from the current queried object?

You are overwriting $post_ids variable on every while loop, never collecting them. That can be solved using $post_ids = array(); while (have_posts()) : the_post(); $post_ids[] = get_the_ID(); ...
user avatar
  • 44.6k
6 votes
Accepted

Multiple User-Meta Values in One User-Meta Key

If you check out the documentation for the update_user_meta() function, you'll note that the $meta_value parameter already accepts objects and arrays, so you can simply save a user's positions in an ...
user avatar
  • 5,180
6 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/
user avatar
6 votes

Save custom post type in different table

I am not going to give any specific code but I will give you Idea how it can be done without creating any custom database table. I am assuming one driver per truck Algorithm Create two html list, ...
user avatar
  • 7,585
6 votes
Accepted

How to use an array of categories as a dropdown?

Try this, $categories_array = array(); $categories = get_categories(); foreach( $categories as $category ){ $categories_array[] = $category->term_id; } array( 'param_name' => '...
user avatar
  • 2,463
6 votes
Accepted

How to check a single number value against an array of meta_key values?

The solution was that I need to compare the literal value of $projectID, so that LIKE compares an exact string instead of just numbers. To make $projectID literal, it needs to be wrapped in quotes. ...
user avatar
  • 91
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' ...
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 ...
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 ...
user avatar
  • 76.1k
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'; ...
user avatar
  • 4,244
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' =&...
user avatar
  • 76.1k
5 votes
Accepted

Save custom post type in different table

While I am a big fan of tweaking wordpress as much as it could because it allows us to, I think the best way forward is to have the best algorithm before coding. Reading your question, I came ...
user avatar
  • 116
5 votes

how to store arrays into a database

What WP does for arrays (and objects) on some contexts (such as post fields) is using maybe_serialize()/maybe_unserialize() to turn such types (and just them) to and from serialized (string-typed) ...
user avatar
  • 97.6k
5 votes
Accepted

if is_singular array not working as expected

You are using an incorrect check here. is_singular() returns true when a post is from the specified post type or post types or the default post types when none is specified. You cannot target specific ...
user avatar
5 votes
Accepted

Show one post per author and limit query to 8 posts

I believe that you can achieve this effect by grouping the query by author ID, which will require a filter(mostly cribbed from the Codex): function my_posts_groupby($groupby) { global $wpdb; $...
user avatar
  • 63.4k
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 ...
user avatar
  • 205
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' ); $...
user avatar
  • 63.9k
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 ...
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 ...
user avatar
  • 147
4 votes

Wordpress: How to use post_class() in an echo

The post_class() function just is a wrapper for get_post_class(). Keep in mind that the latter does not echo, but returns an Array so you have to do something like what post_class() does: $classes = ...
user avatar
  • 49.8k
4 votes
Accepted

Wordpress: How to use post_class() in an echo

The solution has actually been less complicated than I initially thought. The post_class() function does not return a string, but instead it "echoes" the string. Therefore, when you write. echo '<...
user avatar
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('...
user avatar
  • 143
4 votes
Accepted

get_option function

You are using get_option() wrong, first variant, so take another look at the get_option() documentation. Basically you can't directly access an array element with the function, it just doesn't support ...
user avatar
4 votes
Accepted

Creating a unique, linked list of tags from a specific category?

You can build a stack of unique tags, then loop over them again to output. Couple of extra things though - never use query_posts. Secondly, you can be way more efficient in your querying and save a ...
user avatar
  • 34.9k

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