Get posts by category with pure SQL query

If you want to be able to get the category by name, this should work: SELECT DISTINCT substr(post_title, – instr(reverse(post_title), “https://wordpress.stackexchange.com/”) + 1) AS year FROM {$wpdb->posts} p LEFT JOIN {$wpdb->term_relationships} rel ON rel.object_id = p.ID LEFT JOIN {$wpdb->term_taxonomy} tax ON tax.term_taxonomy_id = rel.term_taxonomy_id LEFT JOIN {$wpdb->terms} t ON t.term_id = tax.term_id WHERE post_status=”publish” AND … Read more

Get IDs of posts currently visible on archive

When the wp_enqueue_scripts action fires, the main query has already run and the posts are in the global $wp_query. We just need to grab the ID from each object in $wp_query->posts, the wp_list_pluck function makes that easy: function wpd_get_post_ids(){ if( is_archive() ){ global $wp_query; $post_ids = wp_list_pluck( $wp_query->posts, ‘ID’ ); // $post_ids now contains an … Read more

Possible to get posts from multiple meta keys/values in a single query?

An easy query like the following should work for you: <?php $_query = new WP_Query( array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1, ‘post_status’ => ‘publish’, ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘key1’ ), array( ‘key’ => ‘key2’ ), ), ) ); ?> Note the ‘relation’=>’OR’ in meta_query. More at: WP_Query – Custom … Read more

cron job to auto delete posts of a specific post type older than x days

// add the schedule event if it has been removed if( ! wp_next_scheduled( ‘mg_remove_old_entries’ ) ) { wp_schedule_event( time(), ‘daily’, ‘mg_remove_old_entries’ ); //run the event daily } // action hooked to fired with wordpress cron job add_action( ‘mg_remove_old_entries’, ‘mg_remove_old_entries’ ); function mg_remove_old_entries() { $posts = get_posts( [ ‘numberposts’ => -1, ‘post_type’ => ‘vfb_entry’, ‘date_query’ => … Read more

Date query year and month OR just year

You have not stated how the selection works, but you need to have some kind of conditional set when a specific month is selected and when a year archive is selected. I also do not know where get_the_time comes in or its relevance to the query. But anyways, there are some other really big issues … Read more

How to count get_users query?

when you use get_users() it retrieves an array of users matching the criteria given in $args which means you can simply use PHP’s count() function e.g: $users = get_users($args); $number_of_users = count($users);

search through post-type attachments titles

You should try applying the s(search) as parameter for your custom query. See this example here: $query = new WP_Query( ‘s=keyword’ ); and you can then apply normal loop to iterate through your results. This also performs string match same as like operator %keyword% you mentioned in comment for @Ravs’ answer. Refer documentation here.

$wpdb->get_var not returning a result

$wpdb->get_var returns a single variable, but your SQL statement has SELECT * which returns a row of multiple columns. You need to change your SQL statement to SELECT ID … instead of SELECT * …