Sorting list of sites from multisite network using wp_get_sites

Using get_sites() in WP 4.6+ It looks like wp_get_sites() will be deprecated in WP 4.6. The new replacement is: function get_sites( $args = array() ) { $query = new WP_Site_Query(); return $query->query( $args ); } Very similar to get_posts() and WP_Query. It supports various useful parameters and filters. Here’s what the inline documentation says about … Read more

Reversing the order of posts AFTER the query is performed

I figured out what I was doing wrong. A simple beginners mistake. Array_reverse was working properly, but I wasn’t then reassigning the reversed array back to the $home_shows WP_Query, hence not seeing any change. Here is the answer, and my revised code. <?php $args = array( ‘post_type’ => ‘show’, ‘posts_per_page’ => 5, ‘order’ => ‘DESC’, … Read more

Get list of all registered post types slugs

@EAMann’s answer is correct, but there’s already a build in WordPress function for fetching all registered post types: get_post_types <?php // hook into init late, so everything is registered // you can also use get_post_types where ever. Any time after init is usually fine. add_action( ‘init’, ‘wpse34410_init’, 0, 99 ); function wpse34410_init() { $types = … Read more

Order by & include array by specific post ids

use post__in key instead of include to get posts from the specific post ids. $args = array( ‘post_type’ => ‘testimonials’, ‘posts_per_page’ => 4, ‘orderby’ => ‘ID’, ‘post__in’ => array(883, 563, 568, 106), ); And to order posts by the given posts ids, you can use the following array. $args = array( ‘post_type’ => ‘testimonials’, ‘posts_per_page’ … Read more

Why does get_users suddenly return duplicates?

Seems like a silly question, but are there exactly 2 users in the database? Or are there more. The only thing I can see happening is because you are setting to 3, it’s looping back through. Another thing to try is checking the peterpanpan user, to see if he has the custom_role twice in his … Read more

Nice way to print_r arrays

I created a Kint plugin that works really well. I also integrates with the Debug Bar plugin. You can download it at: http://wordpress.org/extend/plugins/kint-debugger/ There are a few functions to help you out with WordPress specific globals: dump_wp_query() dump_wp() dump_post() For printing arrays in a styled, collapsible format you would do the following. $foo_bar = array( … Read more

Insert new element to array with add_filter

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 … Read more

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 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 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)