33 votes

Multiple orderby values in WP_Query

@Musa how can we put multiple order value for the fields? I was wondering the same question and I found this : In 4.0, you can now pass an array to WP_Query as the value for orderby. The ...
user avatar
  • 441
31 votes
Accepted

Custom post meta field effect on the performance on the post

To answer this, I have gone and done some tests on this, and the results was actually mind blowing. Here is my test To this yourself, set yourself up with a test page. Just simply copy page.php, ...
user avatar
30 votes

Custom query with orderby meta_value of custom field

This issue in general is cleared up as of WordPress 4.2 by using named queries. e.g. $args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( ...
user avatar
30 votes
Accepted

How to display custom field in woocommerce orders in admin panel?

The code you have provided is incomplete. Not sure if that is the only code you are using to achieve what you want. So, besides first code block which you have provided, bellow I am adding all rest of ...
user avatar
30 votes
Accepted

How to fix missing custom fields after upgrading to WordPress 4.8.1?

I don't think it's the WordPress 4.8.1 release doing that: but, do you have Advanced Custom Fields Installed? If you do, their release 5.6.0 hides the default WordPress custom fields meta box by ...
user avatar
  • 2,315
25 votes

Using meta query ('meta_query') with a search query ('s')

I have been searching for hours for a solution to this problem. Array merging is not the way to go, especially when the queries are complex and you must be able to add to meta queries in the future. ...
user avatar
25 votes
Accepted

Can wp_query return posts meta in a single request?

By default, WP_Query returns the standard WP_Post objects for the posts being queried. I believe with some clever rewrite and use of the filters given in WP_Query you can add objects to the returned ...
user avatar
21 votes
Accepted

Difference between meta keys with _ and without _

The underscore prefix are private, these meta fields will be hidden and will not be shown as custom fields in the post backend screens. Those meta fields without the underscore prefix are public ...
user avatar
19 votes

How to get custom post meta using REST API

WP API has a rest_prepare_post filter (or rest_prepare_CPT if you are working with custom posts) which you can use to modify the JSON response. In your case it will be rest_prepare_joblisting. ...
user avatar
18 votes
Accepted

Meta query with string starting like pattern

You could try the REGEXP version: 'meta_query' => array( array( 'key' => 'email_address', 'value' => '^hello@', 'compare' => '...
user avatar
  • 64.1k
17 votes

getting all values for a custom field key (cross-post)

It is not good or needed to use the global $wpdb: // function to grab all possible meta values of the chosen meta key. function get_meta_values( $meta_key, $post_type = 'post' ) { $posts = ...
user avatar
17 votes

Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

Few years later, the code posted by CSSGirl wasn't working for me because there were some posts that didn't have the meta key or the meta key was empty so this is what I had to do to have all the ...
user avatar
16 votes

Is ACF being a honey trap?

Few months ago @tom-j-nowell (one of the mods here) wrote an article explaining the issues with the abuse of meta queries by many WP plugins: https://tomjn.com/2016/12/05/post-meta-abuse/ Among other ...
user avatar
  • 44.7k
13 votes

Extending the search context in the admin list post screen

Stefano answer is great but it lacks a distinct clause : function segnalazioni_search_distinct( $where ){ global $pagenow, $wpdb; if ( is_admin() && $pagenow=='edit.php' && $...
user avatar
13 votes
Accepted

how to filter by last name for custom post

Order by the last word in the post title To order by the speaker's last name, you can use the following setup (PHP 5.4+): // args $args = [ 'posts_per_page' => 10, 'post_type' =...
user avatar
  • 64.1k
13 votes
Accepted

ORDER BY custom field value

You can create groups of meta_queries using specific compare operation on them, and since you want to order based in a single custom field, you can keep the order declaration dedicated to the single ...
user avatar
13 votes
Accepted

How to add tab which is visible only in admin side of product in woocommerce?

I have worked on your issue and found a solution after some Google. Note: Add the below mentioned code to theme's functions.php or any plugin's file. Code: This filter function will add a custom ...
user avatar
12 votes
Accepted

Orderby meta_value only returns posts that have existing meta_key

As stated in @ambroseya's answer, its supposed to work like that. Once you declare a meta query, even if you aren't looking for a specific value, it will only query posts with that meta key declared. ...
user avatar
12 votes

Remove Dimension from wp_get_attachment_image

Workaround I did some core digging/testing and found a workaround through the wp_constrain_dimensions filter: // Add filter to empty the height/width array add_filter( 'wp_constrain_dimensions', '...
user avatar
  • 64.1k
12 votes

Adding a custom field to the site identity menu

You'll have to add your own customizer controls to achieve that. So for example, if you want to add Company Name, you can use this code: function my_register_additional_customizer_settings( $...
user avatar
12 votes
Accepted

How to return Meta data from the REST API?

The first parameter passed to register_meta() is always post for posts, Pages (post type of page) and custom post types. However, the REST API Handbook says that: Prior WordPress 4.9.8, meta ...
user avatar
  • 32.1k
11 votes

Sort posts by custom field numeric value using dropdown

'meta_value_num' is not magic, it casts a value as numeric, however, to be properly cast as numeric, a value must be numeric compatible. In OP it's said that the field values are "numeric" but I bet ...
user avatar
  • 44.7k
11 votes
Accepted

How do I set the default admin sort order for a custom post type to a custom column?

Solution from a cross post over at StackExchange from @birgire: The problem is that you run the clientarea_default_order callback too late. To fix that you only have to change the priority ...
user avatar
10 votes
Accepted

Getting attachments by meta value

I've spent hours of research and found the solution a few minutes after writing this question: $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', <<-- ...
user avatar
  • 321
10 votes
Accepted

Enabling shortcodes for custom fields

Normally Wordpress does not run shortcode that you put into a custom field. By default, Custom Fields display whatever value you enter, as plain-text, so if you try entering a shortcode, (in the ...
user avatar
10 votes

How to enable revisions for post meta data?

I'm sorry, but for now there is no easy way to "enable" revisioning of postmeta. However, you can add post metadata to revisions. A guy named John Blackburn wrote a pretty good article about adding ...
user avatar
9 votes
Accepted

Many to Many Relationship between Two Custom Post Types

As Milo noted, there is no built in way to do a many to many relationship between two custom post types. That being said, these days it isn't too hard to do it yourself without a plugin if wanted. I ...
user avatar
  • 1,862
9 votes

Preserving line breaks when saving and displaying custom fields data

I stuck with the same question and actually tested wpautop(). It worked, e.g. echo wpautop(get_post_meta($post->ID, '_aboutus', true)); Still, I came around one issue. When sanitizing the user ...
user avatar
  • 161
9 votes

Matching Serialized Arrays with meta_query

I just figured out myself how to do it Take a look at this example: $args = array(); $args['relation'] = 'OR'; foreach ( $tag_ids as $t ) { $args[] = array( 'key' => '...
user avatar
  • 409
9 votes
Accepted

compare meta_query in get_posts arguments

If we define the conditions: A: my_custom_field_ignore EXISTS B: my_custom_field_ignore = 1 then NOT ( A && B ) is equivalent to: NOT ( A ) || NOT ( B ) meaning in our case: ( ...
user avatar
  • 64.1k

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