WordPress - Order posts by custom fields (PHP) - php

I would like to order my posts primarily by 'start_date', and secondly by 'start_time' (for posts that have the same 'start_date'). I only know how to order one or the other. Please help!

try adding it after a comma,
=> 'start_date, start_time',

Related

Advanced Custom Field checkbox query not working

I am using an Advanced Custom Field to allocate a custom field to posts.
This field is then used by a filtering system to filter posts using a custom query (via the pre_get_posts action). The other query arguments are working fine, except for the one which relies on ACF’s field.
The current query arguments of the field’s meta_query have been constructed according to the ACF documentation for the field (hence the quotation marks surrounding the value).
Can anyone advise me as to where this is going wrong? The query doesn’t seem to return any posts based on the values I am passing to the query.
Query arguments:
$courses = get_query_var('courses');
if ($courses) {
$query->set('meta_query', array(
array(
'key' => 'course_check',
'value' => '"'.$courses.'"', //i.e mdia-403
'compare' => 'LIKE'
),
)
);
}
ACF stores checkbox values in array, that's the reason your code is not working.
See working example here:
https://support.advancedcustomfields.com/forums/topic/wp_query-using-meta_query-for-an-acf-checkbox-field/
Found the cause of this issue by print_r’ing the entire query and inspecting what was actually going on - had done this a number of times, but overlooked the following detail:
The issue here wasn’t actually due to the checkbox meta_query, but a conflict between the get_query_var term (‘courses’) and a custom taxonomy whose slug is also ‘courses’.
When the query was executed, the query seemed to be querying for posts associated with both the custom taxonomy and the selected checkbox value, yielding no results.
Renaming the name attr to something other than courses then reconfiguring the registered query var and meta_query fixed this issue.

Ordering by multiple meta keys wordpress

Im aware there are several posts relating to this issue but i am yet to find a fix for my current problem.
Im reasonably new to Wordpress and am still getting used to it, however...
The Problem
I'm implementing a front end sorting system for users to filter out schools, i am using a rating plugin (comment rating field pro) and the issue i have is that when users sort by star rating i also want it to sub sort by number of reviews (comments). Currently it is only sorting by rating and simply ignoring the number of comments and sorting by title. I have tried various combinations of query parameter arrays such as meta_query and multiple meta keys but the issue appears to be that it refuses to sort the second value. Both are numbers in the database.
TL:DR it needs to sort by stars and then by number of reviews.
Unfortunatly a custom query is out of the question as the array is being fed into another plugin which appears to only accept an array of args.
This is the sort of idea im going for (this does not work)
Array ( [post_type] => surf-school [posts_per_page] => 12 [paged] => 0 [meta_key] => crfp-average-rating [orderby] => meta_value crfp-total-rating [order] => DESC )
Any help would be greatly appreciated as its driving me insane!
Thanks in advance.
Update
I have managed to fix this using from a comment reply to this post which now appears to have been deleted :(
I managed it by adding a filter to wp_query and forcing a custom order by as seen here Plugin API/Filter Reference/posts orderby - Codex
function edit_posts_orderby($orderby_statement) {
$orderby_statement = "wp_postmeta.meta_value DESC, wp_posts.comment_count DESC";
return $orderby_statement;
}
placed in functions.php and
add_filter('posts_orderby', 'edit_posts_orderby');
placed just before the query. Hope this helps someone else :)

Yii Dataprovider ASC order with reverse output

I'm using Yii's dataprovider + listview now. I'm using this for a mailbox like Facebook. However, I noticed that facebook's mailbox order is neither regular ASC or DESC, it's actually ASC with an inverse order. Like all the conversation is in ASC order but when it comes to page view, you see the last page(newest) first and if you scroll up, you see like next(last) page with order record in ASC order.
How should I do this in Yii's data provider? Since if I get ASC order, my first page will be the oldest page but not newest.
Thanks for your help!
It is better to show some code we can work with in answering your question; but I shall take the shotgun approach and see if I can answer this.
return new CActiveDataProvider('Mail', array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'delivery_time DESC',
),
));
Reference

How to use a relationship in the where parameter when finding records using the pods framework?

my question has to do with the pods framework plugin for wordpress sites. I am using pods version 2.2 and have been having trouble with the where parameter in the find() function.
I'd be suprised if I am the first person to encounter this problem but I have searched extensively and haven't found anyone providing an answer (or question at that).
Anyway, I'll give an example to highlight my problem.
Say I have a Bands Pod and a Records Pod and these two pods have a bi-directional multi-select relationship between them (that is, an n to n relationship). Hence a band can have numerous records and a record can have multiple bands. Further, the relationship exists between the fields BandsPod('records') and RecordsPod('bands').
Now, I can retrieve all records in the records pod like so (note the where is commented out):
$pods = pods('records');
$params = array(
'select' => 't.*',
'limit' => -1
//,'where' => '???'
);
$pods->find($params);
Then do whatever I want with them, e.g. template(), fetch(), etc.
My trouble occurs when I want to filter by band. What should my where statement be if I want to retrieve all records by band with id 1?
My view is that it should be something like this:
$params = array(
'select' => 't.*',
'limit' => -1,
'where' => '1 IN bands.id'
);
$pods->find($params);
However, this does not work (not that I especially expected it to).
It would also be desirable to know how this would work for filtering by multiple bands. E.g.
'where' => '(1, 2) IN bands.id'
As I said before, I've been trying to get this to work for some time and with little joy. I have managed to get it working but in a very ugly way. I.e.
Get bands from Bands Pod with relevant band ids,
Collect all ids of records from the bands pod records field,
Write params for Records Pod
Use the ids in the where statement to check they match with t.id,
$pods->find(); //where $pods=pods('records');
Thanks in advance for taking the time to read this and any answers you may give.
Cheers,
Joe
N.B. I know about the filters() function and it does not do what I'm after. I'd like stuff specifically for the where statement please.
You would want:
'where' => 'bands.id IN ( 1, 2 )'
If your bands are a custom post type, it would be:
'where' => 'bands.ID IN ( 1, 2 )'

Ordering Wordpress posts by id - exact order

it seems i can not find what i am looking for anywhere.
i am using the code below to show posts, using their ids.the problem is that i can't sort them the way i want - i need to order them exactly in order of the ids (currently it is sorting asc or desc.
i don't need that, i only want to show them exactly in the order of the ids)
<?php
$query_args = array('post_type'=>'post', 'category' => '3', 'numberposts'=>'50', 'post_status'=>'publish', 'include' => '100,124,122');
$query_posts = get_posts($query_args);
$last_query = end($query_posts);
foreach ($query_posts as $post) : setup_postdata($post);
any ideas?
I was halfway through writing an answer when I found wyfel's answer to a similar question on the WordPress stack exchange. Short of adding a global to store the IDs (rather than hard-coding them), it's what I planned to suggest (even down to the MySQL FIND_IN_SET function).

Categories