Hot answers tagged

23

$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'post__in', 'post__in' => array(883, 563, 568, 106) ); Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in


23

wpdb's get_results method takes an optional second argument that lets you specify how the data is returned. The default return is an object. But you can also set it to... OBJECT - result will be output as a numerically indexed array of row objects. OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (...


15

Firstly, define your custom cron job schedules. add_filter('cron_schedules', array($this, 'cron_schedules')); public function cron_schedules($schedules){ $prefix = 'cron_';// Avoid conflict with other crons. Example Reference: cron_30_mins $schedule_options = array( '30_mins' => array( 'display' => '30 Minutes', ...


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 ...


12

Filters work by calling each of the hooked callback functions (in priority order). The value to be filtered is passed to the first callback funtion. The returned value of that callback function is then passed to the second callback, and the returned value from that is passed onto third and so on, until all hooked callbacks have been fired. Whatever the last ...


12

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


10

Normally you would just specify a meta_value, e.g.: $args = array( 'post_type' => 'business', 'meta_query' => array( array( 'key' => 'specials', 'value' => 'these are not the specials you are looking for - Obi Wan Kenobi' ) ) ); However because your post meta is ...


10

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 ...


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 ...


8

First can you please confirm that you don't have any caching plugins enabled? Caching plugins can interfere with cron jobs because your visitors are not served a live page but a cached version of your page. If you have a caching plugin enabled, you can choose one of your pages, add an exclussion to your caching plugin's settings for that page so that it is ...


8

You have to collect multiple fields under the same name like this … name="collect[1]" name="collect[2]" … and adjust your widget logic to this. Here is a very simple demo widget: <?php # -*- coding: utf-8 -*- /* Plugin Name: Store Options as array */ add_action( 'widgets_init', array ( 'T5_Array_Options_Widget', 'register' ) ); class ...


8

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 ...


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

For this kind of stuff, I wrote REF (requires PHP 5.3). Among many other features, it displays contents of DocBlock comments and linkifies PHP-internal classes, methods and functions (links point to the PHP manual). It also handles WordPress functions (links point to queryposts.com). Here's the output of $GLOBALS at the setup stage of WordPress in HTML mode ...


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

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

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

get_the_terms() is probably returning false. Do a print_r($event_type_term) to see what you have in it. From: http://codex.wordpress.org/Function_Reference/get_the_terms Array of term objects on success. False if no terms are found in the given taxonomy and a wp_error object if an invalid taxonomy is entered.


4

There is no filter to change that behavior, you would have to replace the entire metabox. On the other hand: I think there is no really simple way to show and to save those arrays. Example for a fictive meta key 'foo': array ( 0 => 2, 'hello' => array ( 0 => 2, 'hello' => 'world' ) ) Creating a default interface ...


4

bloginfo() is using echo, you need a function that returns its value: get_bloginfo(). In this case you could also use just the function get_bloginfo() is using: get_template_directory_uri().


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