wp_query to find posts by year and month

Solved by creating custom sql queries. Request for years: SELECT count(*), $wpdb->posts.id as post_id_ts, YEAR( (SELECT $wpdb->postmeta.meta_value FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = post_id_ts AND $wpdb->postmeta.meta_key = ‘docs_pubdate’)) AS years FROM $wpdb->posts WHERE $wpdb->posts.post_type=”document_base” AND $wpdb->posts.post_status=”publish” GROUP BY years which will return result like this: Array ( [0] => Array ( [count(*)] => 1 [post_id_ts] => … Read more

How to update custom user meta field in wp?

You are using the wrong meta key: update_user_meta( $user_id, ‘Save Posts’, $_POST[‘save_post_external’] ); update_user_meta( $user_id, ‘Save Posts’, $_POST[‘save_post_internal’] ); And that should be: update_user_meta( $user_id, ‘save_post_external’, $_POST[‘save_post_external’] ); update_user_meta( $user_id, ‘save_post_internal’, $_POST[‘save_post_internal’] );

WordPress posts with multiple fields

Unless you’re eager to program the functionality yourself, I would highly recommend “Advanced Custom Fields” (a plugin). It’s very intuitive, and gives you the ability to add custom fields (duh!) to every post-type in your theme. This means that I could add a “Height”-field above my WYSIWIG-editor in my Post-editor.

meta_query for a string inside a meta field containing a comma-separated list

Is there a smart way to meta_query so it matches just “IS, but not “TS-IS”? The WordPress meta query class (WP_Meta_Query), which among others, is used with the posts (WP_Query) and users (WP_User_Query) query classes, supports REGEXP (since WordPress 3.7) as the compare value, so you could use that with a RegEx (regular expresion) pattern … Read more

Only display posts after current date

Turns out meta_query was the way to go. This is the code I used to make it work. add_action( ‘pre_get_posts’, ‘filter_posts’ ); function filter_posts( $query ) { $now = date(“Y-m-d H:i”, strtotime(‘+2 hours’)); if( is_category() && !is_admin() && $query->is_main_query() ) { $query->set( ‘posts_per_page’,’10’ ); $query->set( ‘orderby’,’meta_value_num’ ); $query->set( ‘meta_key’,’date-start-custom’ ); $query->set( ‘meta_query’, array( array( ‘key’ … Read more

Count how many posts have a custom field set

You could run a custom query to count the amount of posts with a given key using $wpdb or more specifically using get_var(). If i assume you want to count published posts with a given key, then something like the folowing should do it..(though this is just an example). function get_meta_count( $key = ”, $type=”post”, … Read more