Sort posts in dashboard using custom field; also include posts where field isn’t set
Sort posts in dashboard using custom field; also include posts where field isn’t set
Sort posts in dashboard using custom field; also include posts where field isn’t set
Try this: <?php $querydetails = ” SELECT wposts.* FROM $wpdb->posts wposts INNER JOIN ( SELECT post_id FROM $wpdb->postmeta wpostmeta WHERE ( ( wpostmeta.meta_key = ‘type’ AND wpostmeta.meta_value LIKE ‘%Collection1%’ ) OR ( wpostmeta.meta_key = ‘type’AND wpostmeta.meta_value LIKE ‘%Collection2%’ ) OR ( wpostmeta.meta_key = ‘type’AND wpostmeta.meta_value LIKE ‘%Collection3%’ ) OR ( wpostmeta.meta_key = ‘type’AND wpostmeta.meta_value LIKE … Read more
The SQL looks correct. (You can substitute new WP_Query for get_posts and echo $my_posts->requests to see it.) So I am going to guess that the sticky posts are making it look as though the sorting doesn’t work. Add ‘ignore_sticky_posts’ => true to your arguments.
This is very crude but should sort your posts by year (meta_value) and then by title. It does depend on how the query is setup so it will only work with the query below or with similar ones. function alter_order_wpse_103181($order,$qry) { remove_filter(‘posts_orderby’,’alter_order’,1,2); $order = explode(‘,’,$order); $order = implode( ‘ ASC,’,$order); return $order; } add_filter(‘posts_orderby’,’alter_order_wpse_103181′,1,2); $q … Read more
you can use following way $attachment_id = 5; //put here you attachment id $sql = “SELECT count(DISTINCT pm.post_id) FROM $wpdb->postmeta pm JOIN $wpdb->posts p ON (p.ID = pm.post_id) WHERE pm.meta_key = ‘_thumbnail_id’ AND pm.meta_value=”$attachment_id” AND p.post_type=”post” AND p.post_status=”publish””; $count = $wpdb->get_var($sql);
Don’t implode $artistNames. The value meta_query argument will take an array if you use any of several compare arguments. You need IN, I believe. value (string|array) – Custom field value. It can be an array only when compare is ‘IN’, ‘NOT IN’, ‘BETWEEN’, or ‘NOT BETWEEN’…. http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters Assuming that $artistNames is an array of ids … Read more
There is no reason that the meta query should not work. The database column is varchar so your number is going to be treated as a string. I tested as best I can without having your database and the testing seems to confirm that. I think you are going to get a lot of spurious … Read more
Set right order for query that returns posts based on two custom fields
You could try this which is mostly code clean up but may solve your problem. I removed the relation from meta_query as it’s only needed if you have multiple meta keys in your query, which you don’t. I’ve also replaced the rewind_posts() with wp_reset_postdata() and finally, you were ending your IF statements earlier than your … Read more
You can only sort on 1 meta value. A few posts (including this SE answer) suggest adding a filter to change the order. Add a function to your theme’s function.php file that will replace the sort order: function change_sort_order( $orderby ) { return str_replace(‘wp_posts.post_date’, ‘mt2.meta_value, mt1.meta_value’, $orderby); } Then use it where you want. Make … Read more