sort query results by newest

WP_Query(array(‘post_type’=>’portfolio_item’, ‘post_status’=>’publish’, ‘posts_per_page’=>-5, ‘order’ => ‘ASC’)); See: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters for order parameters. Hope that helps.

get csv of users with user_meta

SELECT u.ID, u.user_email, u.display_name, um1.meta_value AS first_name, um2.meta_value AS last_name, um3.meta_value AS phone_number, um4.meta_value AS salutation, um5.meta_value AS title FROM wp_users u LEFT JOIN wp_usermeta um1 ON u.ID = um1.user_id AND um1.meta_key = ‘first_name’ LEFT JOIN wp_usermeta um2 ON u.ID = um2.user_id AND um2.meta_key = ‘last_name’ LEFT JOIN wp_usermeta um3 ON u.ID = um3.user_id AND … Read more

Get multiple users with meta value in one query and populate WP_User class

For anyone interested. Got it solved using the get_user_metadata filter to short circuit the metaquery and cache loading. Steps: Run query Store result data in the class property Add get_user_metadata filter Start foreach loop on results Trigger new WP_User( $result_value ) for each result Within the filter function, validate meta key on $wpdb->get_blog_prefix() . ‘capabilities’. … Read more

How to list commenters and days since last commented

It looks like you could add the MAX( comment_date ) last_commented_date to the field list to get the latest date, each user commented. Then use e.g. the human_time_diff() function to get a human readable text for how long ago each user commented. human_time_diff( strtotime( $result->last_commented_date) ) For safety reasons, you should then prepare the SQL … Read more

Wrap every month posts in div

$last_month = null; while ( $the_query->have_posts() ) : $the_query->the_post(); $the_month = get_the_time( ‘Ym’ ); // e.g. 201611 if ( $last_month !== $the_month ) { if ( $last_month !== null ) { // Close previously opened <div class=”row” /> echo ‘</div>’; } echo ‘<div class=”row”>’; } $last_month = $the_month; ?> <div class=”col-3″> <?php the_title(); ?> </div> … Read more

Query posts that contain video?

I don’t think there’s a fool-proofed way to do that, because the YouTube videos can be embedded in various ways and later be removed from the posts’ content. Here are few ideas: If you mean searching for e.g. the youtube string, then you could use the s argument in WP_Query. But the links might be … Read more

pre_get_posts – Trying to get property of non-object warning

$screen may not be available yet when pre_get_posts fires. Try this instead: public function my_post_type_default_sort( $query ){ if(!isset($_GET[‘orderby’]) && is_admin()) { $orderby = ‘0’; } if($query->is_main_query() && is_admin() && is_post_type_archive(‘my_post_type’) && $orderby=0) { $query->set(‘orderby’,’scr_date’); $query->set(‘order’, ‘ASC’); } }

Can’t sort custom post type by random

You could use meta_query to search in the meta data, and then sort them as random: $args = array( ‘posts_per_page’ => 4, ‘post_type’ => ‘projekte’, ‘orderby’=> ‘rand’, ‘post__not_in’ => array( get_the_ID() ), ‘meta_query’ => array( array( ‘key’ => ‘ecpt_skills’, ‘value’ => $projektart, ‘compare’ => ‘LIKE’, ) ), ); There is also a mistype in your … Read more

Loop inside query

This is a PHP syntax error – you can’t nest a foreach inside an array, you use it to build the array: $query = [ ‘post_type’ => $post_type, ‘posts_per_page’ => -1, ‘tax_query’ => [ ‘relation’ => ‘OR’, ], ]; foreach ( $taxonomy_arr as $t ) { $query[‘tax_query’][] = [ ‘taxonomy’ => $t[‘taxonomy’], ‘terms’ => $t[‘value’], … Read more