Wordpress posts order by custom field - posts

I have 2 types of posts - event and news. for event category, i have a custom filed - event_date. but news does not have that field. i want to order the posts according to the event_date (i want to include the news too even though they dont have the event_date field ). i have tried with this:
$args = array(
'cat' => $categories,
'posts_per_page' => $display_count,
'post_type' => 'post',
'number' => $display_count,
'page' => $page,
'offset' => $offset,
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$query = new WP_Query($args);
this query does not return any of the news. is this because the news dont have the order field? if so, then how can i solve this problem? i was thinking of ordering with multiple like-
'orderby'=> 'meta_value_num date'
but no luck.

Related

Get_term with meta_query

I can figure out this query : my purpose is to have a list of taxonomys (category like) with the number posts related to. Something like :
Concerts (4)
Theater (10)
Cinema (12)
...
"Events" posts are store in a CPT 'event' and they all have an "event_start_date" ACF field. If I don't use the meta_query the "get_term" output well all the post by taxonomy. As soon as I try to get all posts from today's date (with the meta_query), nothing appear.
$terms = get_terms( array(
'post_type' => 'events',
'hide_empty' => false,
'taxonomy' => 'events_category',
// 'orderby' => 'name',
// 'order' => 'ASC',
// 'meta_query' => $meta_query_array_from_today,
'meta_query' => array(
array(
'key' => 'event_start_date',
'value' => date( "Ymd"),
'type' => 'date',
'compare' => 'LIKE',
),
)
) );
I've read a lot of answers but i can't make it works, some of answers found do a ne WP-Query but I don't want post, I need a list of taxonomy (using term->count).
If anyone can guide me, I would be grateful.
Thank's !

How get period index status google api?

debug($sitemaps->listSitemaps("http://example.com",
['contents'=> 'indexed']));
return
object(Google_Service_Webmasters_SitemapsListResponse) {
[protected] collection_key => 'sitemap'
[protected] internal_gapi_mappings => array()
[protected] sitemapType => 'Google_Service_Webmasters_WmxSitemap'
[protected] sitemapDataType => 'array'
[protected] modelData => array(
'sitemap' => array(
(int) 0 => array(
'path' => 'http://example.com/sitemap.xml',
'lastSubmitted' => '2017-12-31T17:06:56.592Z',
'isPending' => false,
'isSitemapsIndex' => false,
'type' => 'sitemap',
'lastDownloaded' => '2018-01-03T11:42:39.262Z',
'warnings' => '0',
'errors' => '0',
'contents' => array(
(int) 0 => array(
'type' => 'web',
'submitted' => '4',
'indexed' => '0'
)
)
)
)
)
[protected] processed => array()
}
modeldata should return an array of data with dates and indexing values
I fear it's not part of the Search Console API as of yet. There's this post on the Google Product Forum as well: https://productforums.google.com/forum/?hl=en#!topic/webmasters/KIUsaXpquKY;context-place=topicsearchin/webmasters/category$3Awebmaster-tools%7Csort:relevance%7Cspell:false
I haven't seen any changes since then, so I wouldn't hold my breath if I were you.
The closest you can get to some number from the API, indicating the amount of pages indexed by Google, is by summing all the contents[].indexed of all the sitemaps and/or sitemap indexes that you have submitted. Probably you only submitted one sitemap index that references all sitemaps, so if you request that one using Sitemaps:get (https://developers.google.com/webmaster-tools/search-console-api-original/v3/sitemaps/get) you get the total of pages indexed.
But that number is poorly correlated to the actual Index status, that you can find in the Search Console UI, in ways that I don't properly understand.
I would think that if you have a sitemap.xml that contains every page of your website, the sum of contents[].indexed would be closely math the total number of pages indexed. That doesn't seem to be the case.

wordpress WP Query and Custom Field filtering

I've encoutered problem with filtering pages to display in WP query.
I use Codex WP query reference for custom fields with ACF (Advanced Custom Fields plugin - but it doesn't matter, couse it works same as WP custom field) parameters to filter pages.
In "Multiple Custom Field Handling" paragraph, Codex got an example with 2 conditions. We can use OR or AND relation. I works for both until you have 3rd condition (array).
They use example:
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
It has only 2 arrays. When I put 3rd, nothing shows. In debug mode I can see an error:
WordPress database error: [Lost connection to MySQL server during query]
When I use AND it works, but I got to use OR. Unfortunately it makes MySQL disconnect.
I've tryed asking phpMySQL for same query WP does. It couses problem - phpMyAdmin says same:
Lost connection
Any Idea?
Maybe I should try different aproach for filtering? (maybe I should use taxonomy?)
Here is code I use:
$query_array = array('relation' => 'OR');
array_push($query_array,
array(
'key' => 'filter1',
'value' => 'value1',
'compare' => 'LIKE'
),
array(
'key' => 'filter1',
'value' => 'value2',
'compare' => 'LIKE'
),
array(
'key' => 'filter1',
'value' => 'value3',
'compare' => 'LIKE'
)
);
$args = array(
'order' => $order_array,
'meta_key' => $meta_key,
'orderby' => $orderby,
'post_type' => 'page',
'paged' => $paged,
'post__in' => $postIDs,
'posts_per_page' => 12,
'paged' => get_query_var('paged'),
'meta_query' => $query_array
);
query_posts($args);
?>
(variables for $args are set of course)
I don't know why I can't use
'compare' => '='
but probably it is why I can't use:
$query_array = array('relation' => 'OR');
array_push($query_array,
array(
'key' => 'filter1',
'value' => array('value1', 'value2', 'value3'),
'compare' => 'IN'
)
);
Just wanted to say that your comment helped me; I'd been butting my head against a very similar problem for a while. I'm using ACF too, and using it to attach items of one post type to another custom post type was easy - for instance, to Attach Person_1 and Person_3 to "Project_A".
This made it easy to list out which users were attached to specific projects. But when it came to do the same in reverse - to show which projects were attached to which users - it became a massive headache.
I finally figured it out, in part thanks to your comment - I'll post my solution here in case someone else comes along with the same problem:
$args = array(
'numberposts' => -1,
'post_type' => 'project',
'meta_query' => array(
'relation' => 'IN',
array(
'key' => 'people',
'value' => ';s:1:"' . $person->ID . '";',
'compare' => 'LIKE'
)
)
);
In short: because the ACF values in repeater fields et cetera are serialized, the compare keyword has to be "LIKE", and I added some context to the value to eliminate false returns - just searching for an ID like "1" would match a lot of the (wrong) posts, but the ";s1;" part ensures that the given value is at index 1, which in my case is the correct index.
So it would need tweaking from case to case. Inspecting what you're trying to match up with var_dumping "get_post_meta($post->ID, 'people')" is helpful for getting the value correct.

Cakephp find order by an associated model

After spending few hours to find a solution for the following issue I´m hoping you can help me.
I have two tables:
Event hasmany Appointments and Appointment belongsto Event
Now I perform in my EventsController:
$this->Event->find('all')
Now I want to order the Events by Appointments. So that the Event with the earliest Appointment is first in the array and so on. I´ve tried many ways, with Containable, Joins, Grouping, Subselects but nothing works.
To perform the find('all') on $this->Event->Appointment is not a solution due to the fact that I want every Event just ones.
Edit #1:
My only solution so far was
$events = $this->Event->find('all', array(
'joins' => array(
array(
'table' => '(SELECT event_id, MIN(start) start FROM appointments GROUP BY event_id)',
'alias' => 'Appointment',
'type' => 'LEFT',
'conditions' => array(
'Event.id = Appointment.event_id'
)
)
),
'order' => array(
'Appointment.start ASC'
)
));
But this is not the best solution!
An option is to create a virtual field representing the earliest appointment date that you can sort by
Put this in your Event model:
function __construct($id = false, $table = null, $ds = null) {
$this->virtualFields['firstappt'] = 'SELECT MIN(Appointment.created) FROM appointments AS Appointment WHERE Appointment.event_id = Event.id';
parent::__construct($id, $table, $ds);
}
You should be able to use this code in your controller:
$this->Event->find('all',array('order' => array('firstappt')));

Grabbing Facebook life events

Ive been looking for a way to pull recent life events for friends from the graph API but I've ran into a blocker for locations.
I know you can query friends for their location. But is it possible to find previous location history (or just history in general, if someone was given a promotion at work so they change their role?).
Excuse the lack of code as I'm posting from my mobile.
What I have for locations is:
->api('/me/friends?fields=name,location);
Which works fine for current location.
Any tips would be great. Thanks.
So it turns out the best way to do this is to use FQL against the "stream" table. From here we can add WHERE clauses to do exactly what I'm after. The hardest part is the lack of documentation provided by Facebook on a specific field. "Type".
https://developers.facebook.com/docs/reference/api/user/
https://developers.facebook.com/docs/reference/fql/stream/
Permissions required: "read_stream"
Doing the initial query "SELECT post_id, actor_id, target_id, message FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0" gives you a wide list of updates within your news stream.
So by changing a few things on my test account I was able to find the "type"'s that matched the activity changes on a users account.
Work update - 305
Education update - 305
Work and Education are grouped.
Relationship status - 62
Changed locations - 282
So say if we wanted to be specific and look for one activity we can change the "type" to be equal to the id's mentioned above.
$query = "SELECT post_id, actor_id, target_id, message, created_time, description, description_tags, type, place FROM stream WHERE type = '62' filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me())";
$response = $this->facebook->api('/fql?q=' . urlencode($query));
The response will look something like this:
'data' => array(
(int) 0 => array(
'post_id' => '100035395_38758242', //I've changed this post id to be random
'actor_id' => (int) 4242, //I've changed this actor_id to be random
'target_id' => null,
'message' => '',
'created_time' => (int) 1347601178,
'description' => 'Joe Smith updated his work and education on his timeline.',
'description_tags' => array(
(int) 0 => array(
(int) 0 => array(
'id' => (int) 4242, //I've changed this id to be random
'name' => 'Joe Smith',
'offset' => (int) 0,
'length' => (int) 8,
'type' => 'user'
)
)
),
'type' => (int) 305, //<--- Note the type id
'place' => null
),
);
Now we need to be aware that Education and Work history can be combined, and potentially other types can be as well. We can from here query the "post_id" directly to get more information related to it.
$post = $this->facebook->api('/100035395_38758242');
The response of this could be something like:
array(
'id' => '100035395_38758242',
'from' => array(
'name' => 'Joe Smith',
'id' => '4242'
),
'story' => 'Joe Smith added Harvid University of America to his timeline.',
'story_tags' => array(
(int) 0 => array(
(int) 0 => array(
'id' => '4242',
'name' => 'Joe Smith',
'offset' => (int) 0,
'length' => (int) 8,
'type' => 'user'
)
)
),
'actions' => array(
(int) 0 => array(
'name' => 'Comment',
'link' => 'http://www.facebook.com/3535/posts/345345' //Changed
),
(int) 1 => array(
'name' => 'Like',
'link' => 'http://www.facebook.com/345345/posts/34534' //Changed
)
),
'type' => 'link',
'created_time' => '2012-09-14T05:39:38+0000',
'updated_time' => '2012-09-14T05:39:38+0000',
'comments' => array(
'count' => (int) 0
)
)
The alternative could be to also query the user id directly and pull fields relevant to the "type". I.e. 'work' or 'education'.
$friend = $this->facebook->api('/242?fields=work,name,education,id');
NOTE: If you require data from X Weeks or Months ago you should use the "created_time" within your queries to Facebook, otherwise you're limited to: 30 days or 50 posts per query.