Hot answers tagged

13

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 wp_get_attachment_image( get_the_ID(), 'thumbnail', "", ["class" => "my-custom-class"] ); ?> The main advantage of this aproach is that you will also get the whole ...


13

You can use get_the_title() to return the current post title in the loop.


13

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 posts_per_page Always set a maximum, even if it's one you never expect to reach. -1 is asking for trouble: There's no upper limit, so the DB keeps working even after you ...


10

No, this won't work: <?php echo get_option('notice_data[Message]'); ?> Because get_option pulls whole option value by option_name, it doesn't pull by pieces of the serialized array. What you are asking for is a key (option_name) called literally notice_data[Message]. Assuming you've saved the option as notice_data you aren't going to get a match, and ...


9

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 this: $family = [ 'father' => [ 'John', 'Doe', 40 ], 'mother' => [ 'Jane', 'White', 39 ], 'child_1' => [ 'Luke', 'Doe', 5 ] ]; If you store ...


7

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' => -1, 'post_type' => 'product', 'meta_key' => 'wppl_is_dax', 'meta_value' => '1' 'fields' => '...


6

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(); endwhile; var_dump($post_ids); // this is an array of ids However there is simpler way, you can skip the whle cycle and simply run: if( function_exists( '...


6

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 array without any additional effort: update_user_meta( 22, 'position_names', array( 'Khaleesi of the Great Grass Sea', 'Breaker of ...


6

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, make them sortable. Each list items represents a post type and have ID data field. Once sorting is done get all ids in order. Make two array one contains truck ...


6

Try this, $categories_array = array(); $categories = get_categories(); foreach( $categories as $category ){ $categories_array[] = $category->term_id; } array( 'param_name' => 'category_id', 'type' => 'dropdown', 'value' => $categories_array, // here I'm stuck 'heading' => __('Category filter:', 'overmax'), ...


6

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. So, I changed this line: 'value' => $projectID, to: 'value' => '"'.$projectID.'"', Which solves the problem.


6

@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 https://developer.wordpress.org/reference/functions/get_sites/ $mysites = get_sites( [ 'public' => 1, 'number' => 500, '...


6

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 we can check in_array: $this_id = 42; if( in_array( $this_id, $term_ids ) ){ // do something }


5

Searching inside a serialized array is difficult and inefficient-- ie. slow. The following pure SQL will do it: SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'daysonair' AND `meta_value` LIKE '%thursday%' LIMIT 0 , 100 Something like this should get WP_Query to do something similar: $args = array( 'post_type' => 'post', 'meta_query' => array(...


5

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) representation. While this simplifies workflow it comes with penalties, such as being unable to properly query through such data and common issues with migration (...


5

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 single posts with is_singular() You have to use is_single to target a specific post if ( is_single( 'post-a' ) { // Do something for post-a } elseif ( ...


5

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; $groupby = "{$wpdb->posts}.post_author"; return $groupby; } add_filter( 'posts_groupby', 'my_posts_groupby' ); If you have less than 8 authors, however, ...


5

Try this php built-in function array_walk_recursive function wpse_do_something_on_data() { $data = array( 'repeater-1' => array( array( 'user_defined_field1' => 'http://www.domain-001.com', 'user_defined_field2' => 'http://www.domain-002.com', ), array( '...


5

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 feedid as key: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results


5

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' ); $names = wp_list_pluck( $terms, 'name' ); where we pluck out the wanted field into an array.


5

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 still use normal loop: <?php $args = array( 'post_type' => 'post', 'posts_per_page' => 3, 'post_status' => 'publish' ...


5

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'; update_post_meta( $post_id, 'themeOps', $kisiArray ); Update (sort by custom field) $args = array( 'post_type' => 'post', 'meta_key' => '...


4

This is a serialized value, so you should run it through maybe_unserialize() or just unserialize() before you edit it. When you work in plugins or themes, always use the API: update_option() and get_option() for example. These functions will un/serialize the values for you, so you don’t have to worry about the database. See also: Settings API with arrays ...


4

array_merge does not work the way you expect/need it to, but honestly I am not sure why you are making this so complicated. All you need is: $args = array( 's' => 'keyword1 keyword2', 'orders' => 'DESC', 'showposts' => 60 ); // var_dump($args); // debug $query = new WP_Query($args); var_dump($query->request); // debug The built ...


4

So I came back to this after practice and implemented some of the Alchemy setup, meaning all the inputs have names like name="_movies[3][title]" where 3 is the iteration we're on in the loop. Create the Metabox // metaboxes should be registered on the add_meta_boxes hook add_action('add_meta_boxes', 'add_meta_boxes' ); function add_meta_boxes() { ...


4

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 '<div ' . post_class() . '>'; The effect is the following: First, the system execute post_class(), generating an "echo ...." with the class attribute second,...


4

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 across " Actually, I have created two custom post type named 'truck' and 'drivers' ". Issue: Linking two post types requires extra work as you are doing because ...


4

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('hide_empty' => false)); foreach( $categories as $category ){ $categories_array[$category->name] = $category->term_id; }


4

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 it. The second variant should be possible, but you need at least PHP 5.4 - if I'm not totally mistaken; I don't know anything about dreamweaver. The reason ...


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