We changed our privacy policy. Read more.
30

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, rename it and delete the loop. Now just create a new page in the back end. Before you start, first test your timer with empty info to get the amount of queries ...


29

@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 syntax looks like: $q = new WP_Query( array( 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) )); Have a ...


29

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 the code which is required to show the new field on backend in 'Order Details' box and make it editable through custom fields. Please note, in your second code ...


29

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 default. remove_wp_meta_box We added in a new setting in v5.5.13 allowing the default WP custom field metabox to be removed. Although simple, this can ...


28

Well, you are using: get_post_meta(get_the_ID(), 'subtitle', TRUE); So, you are saying to Wordpress to get the meta value of the 'subtitle' field and that the returned value be in format of string. See get_post_meta() docu. To get all meta data of a post you should use get_post_custom() function instead. For example, if you are inside the loop: $custom = ...


27

This issue in general is cleared up in WordPress 4.2 by using named queries. e.g. $args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive', 'value' => 'some_value', 'type' => 'NUMERIC' // unless ...


24

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. The solution which is simplistically beautiful is to change 's' to one which allows both searching titles and meta fields. add_action( 'pre_get_posts', function(...


23

Thank you everyone for your help! In the end the query below got me the results I desired - which was to show and sort the posts by a custom field of "publication_date" first - sorting by the date, and if there were multiple of the same date (say, 4 marked June 2013), it would sort those by title. Then, after it runs through all the posts that have the ...


22

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 WP_Post objects array. Will this be performant? In my opinion, it will hurt performance more as you will need to join results in your query as custom fields ...


19

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 fields and shows up as custom fields in the post screens


18

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. function filter_joblisting_json( $data, $post, $context ) { $phone = get_post_meta( $post->ID, '_phone', true ); if( $phone ) { $data->data['phone'] = $...


17

You could try the REGEXP version: 'meta_query' => array( array( 'key' => 'email_address', 'value' => '^hello@', 'compare' => 'REGEXP', ) ) where ^ matches the beginning of a string. You can check the MYSQL reference on REGEXP here for more info. Notice that these are the ...


15

Here's a pastebin with your code included. Get the old value of the tinyMCE $meta_biography = get_post_meta( $post->ID, 'meta_biography', true ); Call the TinyMCE Editor wp_editor( $meta_biography, 'biography', array( 'wpautop' => true, 'media_buttons' => false, 'textarea_name' => 'meta_biography', 'textarea_rows' => ...


15

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 posts ordered by date and show the ones with a meta key value display first: $args = array( 'post_type' => $type, 'post_status' => 'publish', '...


15

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 things he says there: [...] sites have been brought down by this, and it’s the reason a number of popular plugins don’t scale to high traffic [...] So, yes,...


14

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 = get_posts( array( 'post_type' => $post_type, 'meta_key' => $meta_key, 'posts_per_page' => -1, ) ); ...


13

Stefano answer is great but it lacks a distinct clause : function segnalazioni_search_distinct( $where ){ global $pagenow, $wpdb; if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='segnalazioni' && $_GET['s'] != '') { return "DISTINCT"; } return $where; } add_filter( 'posts_distinct', '...


12

Although you should have to add some validation, this action does not seem to be complicated for the current version of WordPress. Basically you need two steps to add a Custom Field to a Custom Post Type: Create a metabox which holds your Custom Field Save your Custom Field to the database These steps are globally described here: http://wordpress.org/...


12

A GROUP BY statement can group your posts after the JOIN. For Wordpress you can use the posts_groupby filter. add_filter( 'posts_groupby', 'my_post_limits' ); function my_post_limits($groupby) { global $pagenow, $wpdb; if ( is_admin() && $pagenow == 'edit.php' && $_GET['post_type']=='listings' && $_GET['s'] != '' ) { $...


12

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. If you want to include all posts sort them by the meta key, use the following code: $args = array( 'post_type' => 'news', 'orderby' => '...


12

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', '__return_empty_array' ); // Display image html echo wp_get_attachment_image( $entry['slide_image_id'], 'full', true ); // Remove filter again remove_filter( '...


12

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 meta field. So: $q = new WP_Query( array( 'meta_key' => 'population', //setting the meta_key which will be used to order 'orderby' => ...


11

'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 there's something not numeric, e.g. a currency symbol like "10 $" or "$ 10.00", or other symbol like "10-". Also consider that using a thousands separator most ...


11

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 from the default one that's 10: add_action( 'pre_get_posts','clientarea_default_order'); to the priority of 9: add_action( 'pre_get_posts','...


11

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' => 'speaker', 'meta_key' => 'speaker-front-page', 'meta_value' => '1', 'orderby' => 'wpse_last_word', //&...


11

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 tab to the Products Data metabox <?php add_filter( 'woocommerce_product_data_tabs', 'add_my_custom_product_data_tab' , 99 , 1 ); function ...


11

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( $wp_customize ) { $wp_customize->add_setting( 'my_company_name', array( 'default' => '', 'type' => 'option', // you ...


11

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 fields set to show_in_rest using register_meta are registered for all objects of a given type. If one custom post type shows a meta field, all custom post types ...


9

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 do many to many relationships between custom post types by using non-singular post meta. e.g. Books and Authors Stick an author_id post meta (non-singular) ...


9

A lot of code can be reduced by using a modified version of this answer. $q1 = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 's' => $query )); $q2 = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'speel', ...


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