18
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
...
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.
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 ...
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 ...
7
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/
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.
...
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' ...
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 ...
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 ...
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';
...
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' =&...
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 ...
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;
$...
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 ...
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' );
$...
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 ...
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('...
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 ...
4
votes
Accepted
How to decipher the following array
You're looking at a serialized representation of the array Array( '75', '68' ). Serialization is the process by which PHP stores a data object as a string, much like the manner in which JSON is a ...
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 ...
4
votes
Accepted
How to use json_decode a post_meta value?
The values look like they have been serialized using maybe_serialize( $data ).
If that's the case, then you can reverse that with maybe_unserialize( $original ).
As per this answer, unserialize( $...
4
votes
Accepted
WP Query for variable taxonomies
tax_query requires an array of arrays, and $the_taxes is an array of arrays, you've already got 99% of your answer.
foreach ($taxes as $tax) {
$the_taxes[] = array (
'taxonomy' => $...
4
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='...
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 ...
4
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'] == '...
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 ...
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:
...
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. (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
array × 757php × 211
wp-query × 100
functions × 60
post-meta × 60
custom-field × 56
loop × 51
custom-post-types × 45
categories × 40
meta-query × 36
posts × 30
user-meta × 30
terms × 28
options × 27
shortcode × 25
advanced-custom-fields × 25
query × 23
plugins × 22
query-posts × 21
custom-taxonomy × 20
filters × 20
metabox × 20
wpdb × 19
meta-value × 19
database × 18