Hot answers tagged

11

Object cache everywhere WordPress tries to reduce the number of database queries as much as possible. For example, anytime you get a meta field or a taxonomy field, before querying the database, WordPress looks if that that was already queried and stored in cache, and returns it from there instead of querying the database. The "cache job" is done via ...


4

This part of wp_insert_posts() gives it away: if ( ! empty( $postarr['meta_input'] ) ) { foreach ( $postarr['meta_input'] as $field => $value ) { update_post_meta( $post_ID, $field, $value ); } } where we see how the post meta fields are updated/added with update_post_meta(). Here's the inline description for ...


3

The main point of interest here is the update_post_caches function. It is called after WP_Query got all the posts from the DB. Usually, the reason you want the posts in the first place is to display them which usually means to display the terms and something based on the metadata, therefor WP_Query will also by default query the DB for the meta and term data ...


3

Your syntax is complete wrong in your meta_query. A key/value pair in an array should be in the format of 'key' => 'value'. Your format is 'key', 'value' which in context of an array translate to 0 => 'key', 1 => 'value'. That is why your meta_query does not work. Your failure is also due to not very good housekeeping. As your meta_query is ...


2

To begin with you may need to add %s to the sprintf function, as the current link has no anchor text and this is supposed to be put in by sprintf as the second argument: return sprintf( '<div class="form-block block-after-indent"><a class="atbtn atbtn--secondary1" href="#sime_url_product_id_%d">%s</a></div>', $product->id, ...


1

A short little function, perhaps: function get_post_meta_single( $id ) { $m = get_post_meta( $id, false, true ); // get post meta as singles foreach( $m as &$v ) $v = array_shift($v); // collapse value array return $m; } As a side note, it's likely that the reason that WordPress doesn't collapse the array is to maintain consistency, so the ...


1

get_the_ID() can (only) be used within the loop. This retrieves the ID of the current post handled by the loop. You can use it on it's own if you need it only once: $dish_meta = get_post_meta( get_the_ID(), 'dish_meta', true ); You can also store it as a variable if you need it more than once: $post_id = get_the_ID(); $dish_meta = get_post_meta( ...


1

This kind of meta query in WP_Query() or get_posts(): 'meta_query' => [ [ 'key' => 'score', 'value' => 50, 'type' => 'numeric', 'compare' => '>=', ], ], would generate the following SQL WHERE part: wp_postmeta.meta_key = 'score' AND CAST(wp_postmeta.meta_value AS SIGNED) >= '50' ...


1

If you want both posts that have and do not have a meta key, you need an OR relation meta query to select both posts with key and those where it does not exist. $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => '_post_like_count', 'compare' => 'EXISTS' ), array( ...



Only top voted, non community-wiki answers of a minimum length are eligible