Ordering by meta_value AND date NOT WORKING with wp_query

You have to use the posts_orderby filter to do this at the mo, eg function wpse159469_posts_orderby( $orderby, $query ) { return implode( ‘ DESC,’, explode( ‘,’, str_replace( array(‘ ASC’, ‘ DESC’ ), ”, $orderby ) ) ) . ‘ DESC’; } then around your query: add_filter( ‘posts_orderby’, ‘wpse159469_posts_orderby’, 10, 2 ); $query = new WP_Query( … Read more

Autoprediction / Autocomplete Search with Meta Keys

If you want to show custom field values, you can’t use WP_Query that select posts. It seems to me that the only available way is to write a sql query and execute it in $wpdb. global $wpdb; if ( isset($_POST[‘search’]) == false || empty($_POST[‘search’]) ) wp_send_json_success( $array() ); $s = $_POST[‘search’]; $meta_keys = [‘PLZ’, ‘ort’, … Read more

Add a default meta_value to new posts

Add this to your functions.php in your template folder. <?php //Set Default Meta Value function set_default_meta_new_post($post_ID){ $current_field_value = get_post_meta($post_ID,’YOURMETAKEY’,true); //change YOUMETAKEY to a default $default_meta=”100″; //set default value if ($current_field_value == ” && !wp_is_post_revision($post_ID)){ add_post_meta($post_ID,’YOURMETAKEY’,$default_meta,true); } return $post_ID; } add_action(‘wp_insert_post’,’set_default_meta_new_post’); ?>

WP_query : meta_key with custom rule for specific value

A meta_query is an array of arrays. Look at the examples in the the Codex. $args = array( ‘post_type’ => ‘my_custom_post_type’, ‘meta_key’ => ‘age’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ‘meta_query’ => array( array( ‘key’ => ‘age’, ‘value’ => array(3, 4), ‘compare’ => ‘IN’, ) ) ); $query = new WP_Query($args); What you have is … Read more

Using get_terms() with meta_query parameters

Inserting boolean term meta values When we add non-existent term meta with e.g. add_term_meta( 123, ‘test’, true ); then we are actually running the following insert : $wpdb->insert( ‘wp_termmeta’, array( ‘term_id’ => 123, ‘meta_key’ => ‘test’, ‘meta_value’ => true ) ); within the general add_metadata() function. Now wpdb::insert() is actually a wrapper for wpdb::_insert_replace_helper() that … Read more

Help With issue on pre_get_posts filter in taxonomy

Solved! I was using the wrong technique to create a custom query to my taxonomy instead of using the function above I create new function that use query_vars. Here is the code function taxonomy_posts_order($query) { global $browsetype; $term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) ); if ( $query->query_vars[‘taxonomy’] == $term->slug ) { … Read more

Order by custom field date with ASC order

With some more googling: <?php $today = date(“Y/m/j”); query_posts(array( ‘post_type’ => ‘custompost’, ‘posts_per_page’ => 4, ‘meta_key’ => ‘mydate’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘ASC’, ‘meta_query’ => array( array( ‘key’ => ‘mydate’, ‘meta-value’ => $value, ‘value’ => $today, ‘compare’ => ‘>=’, ‘type’ => ‘CHAR’ ) ) )); if (have_posts()) : while (have_posts()) : the_post(); ?> <a … Read more