Im trying to filter my next_post_link and previous_post_link so it can be used on a custom post type called filmfremvisning with a meta_key field with a date in it. The filmfremvisning CPT is some kind of "events" CPT where the event posts have an expiration date on them. The expiration date has the following format of YYYYMMDD and is named dato_for_fremvisningen.
I also have a custom post status called expired and a cronjob that gives all expired posts that status every night.
I have tried these two links:
https://wordpress.stackexchange.com/questions/139453/filter-next-post-link-and-previous-post-link-by-meta-key
http://return-true.com/2012/04/custom-ordering-for-wordpress-post-link-functions/
And I got it working quite nice. The problem however is that the INNER JOIN is skipping posts that has the same date.
For example if I have the following posts and dates:
Event 1 - 20140102
Event 2 - 20140105
Event 3 - 20140105
Event 4 - 20140107
It returns:
Event 1 - 20140102
Event 2 - 20140105
Event 4 - 20140107
The SQL statement is for furture post is:
INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id
WHERE p.post_type = 'filmfremvisning'
AND p.post_status = 'publish'
AND m.meta_key = 'dato_for_fremvisningen'
AND m.meta_value > '$current_filmfremvisning_date'
GROUP BY p.ID ORDER BY m.meta_value ASC`
The code im using in functions.php is down below:
function get_adjacent_past_events_join($join) {
if(is_singular('filmfremvisning')) {
global $wpdb;
$new_join = $join."INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id ";
return $new_join;
}
return $join;
}
add_filter('get_previous_post_join', 'get_adjacent_past_events_join');
add_filter('get_next_post_join', 'get_adjacent_past_events_join');
function get_future_filmfremvisnings_where($where) {
if(is_singular('filmfremvisning')) {
global $wpdb, $post;
$id = $post->ID;
$current_filmfremvisning_date = get_field('dato_for_fremvisningen', $id);
$new_where = "WHERE p.post_type = 'filmfremvisning' AND p.post_status = 'publish' AND m.meta_key = 'dato_for_fremvisningen' AND m.meta_value > '$current_filmfremvisning_date'";
return $new_where;
}
return $where;
}
add_filter('get_next_post_where', 'get_future_filmfremvisnings_where');
function get_past_filmfremvisnings_where($where) {
if(is_singular('filmfremvisning')) {
global $wpdb, $post;
$id = $post->ID;
$current_filmfremvisning_date = get_field('dato_for_fremvisningen', $id);
$new_where = "WHERE p.post_type = 'filmfremvisning' AND p.post_status = 'publish' AND m.meta_key = 'dato_for_fremvisningen' AND m.meta_value < '$current_filmfremvisning_date'";
return $new_where;
}
return $where;
}
add_filter('get_previous_post_where', 'get_past_filmfremvisnings_where');
function get_prev_past_filmfremvisnings_sort($sort) {
if(is_singular('filmfremvisning')) {
global $wpdb;
$new_sort = " GROUP BY p.ID ORDER BY m.meta_value DESC";
return $new_sort;
}
return $sort;
}
add_filter('get_previous_post_sort', 'get_prev_past_filmfremvisnings_sort');
function get_next_future_filmfremvisnings_sort($sort) {
if(is_singular('filmfremvisning')) {
global $wpdb;
$new_sort = " GROUP BY p.ID ORDER BY m.meta_value ASC";
return $new_sort;
}
return $sort;
}
add_filter('get_next_post_sort', 'get_next_future_filmfremvisnings_sort');
Related
I am using this to get the data from the latest completed order:
<?php
function get_last_order_id(){
global $wpdb;
$statuses = "wc-completed";
// Getting last Order ID (max value)
$results = $wpdb->get_col( "
SELECT MAX(ID) FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND post_status IN ('$statuses')
" );
return reset($results);
}
$latest_order_id = get_last_order_id(); // Last order ID
echo ($latest_order_id);
$order = wc_get_order( $latest_order_id );
$order_data = $order->get_data();
print_r($order);
However, the data that I need to get is showing as this:
[data:protected] => Array ( [id] => 3767 [key] => billing_name [value] => David) )
I have searched the questions on the StackOverflow on this but unfortunately, I still can't access this. Does anyone have an idea of how I can get it?
Thanks in Advance
<?php
function get_last_order_id(){
global $wpdb;
$statuses = "wc-completed";
// Getting last Order ID (max value)
$results = $wpdb->get_row( "SELECT MAX(P.ID) ,PM.meta_value as firstname
FROM {$wpdb->prefix}posts AS P
INNER JOIN {$wpdb->prefix}postmeta AS PM
ON P.ID = PM.post_id
WHERE P.post_type LIKE 'shop_order'
AND P.post_status IN ('$statuses')
AND PM.meta_key='_billing_first_name'" );
return $results;
}
$latest_order_id = get_last_order_id(); // Last order ID
$firstname = $latest_order_id->firstname;
echo $firstname;
I try to get the post_ids from postmeta where the _sale_price_dates_to are less the now. And I did something like this:
global $wpdb;
$publishid = $wpdb->get_results('SELECT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key="_sale_price_dates_to" and meta_value<=' .time().'') or die("Error: Cannot create object");
But it doesn't work. Maybe because the _sale_price_dates_to has format longtext not date. how to change it in the query?
But It's not over. I want to add the condition that the products have to be published then I have to join:
$publishid = $wpdb->get_results('SELECT meta_value FROM ' . $wpdb->postmeta . 'join' . $wpdb->posts . ' ON wp_posts.ID = wp_postmeta.post_id WHERE wp_postmeta.meta_key="_sale_price_dates_to" and wp_postmeta.meta_value<=' .time().' and wp_posts.post_status="publish" ') or die("Error: Cannot create object");
But because of the first problem, I don't know it works.
Update: Setting up the right time zone in the code
Try the following to make a functional SQL query using WPDB to get the Post IDs where post meta _sale_price_dates_to timestamp is less the now time stamp:
global $wpdb;
// Set the correct time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/Paris');
// An array of IDs
$results = $wpdb->get_col("
SELECT p.ID
FROM {$wpdb->prefix}posts AS p
JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_status = 'publish'
AND pm.meta_key = '_sale_price_dates_to'
AND pm.meta_value <= '".time()."'
AND pm.meta_value != ''
");
// Raw Output (array of post IDs)
print_r($results);
Tested and works.
meta_value != '' is needed to avoid Post IDs with empty values to be queried.
I need to get the pages parent template name. I know I can use get_page_template() for the current page, but there doesn't seem to be a way to get the parents one.
Is it also possible to get just the templates name instead of the path to it?
Yeah, get_page_template() can only return the file path of the current page. Use get_page_template_slug() instead, which returns just the file-name and can accept a post ID as an argument. You can combine it with wp_get_post_parent_id() to get the parent page's ID.
get_page_template_slug(wp_get_post_parent_id(get_the_id()))
You can try this for get parent page template name
/********** GET PAGES BY PARAMS ************/
/*-- Get root parent of a page --*/
function get_root_page($page_id)
{
global $wpdb;
$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
if ($parent == 0)
return $page_id;
else
return get_root_page($parent);
}
/*-- Get page name by ID --*/
function get_page_name_by_ID($page_id)
{
global $wpdb;
$page_name = $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID = '$page_id'");
return $page_name;
}
/*-- Get page ID by Page Template --*/
function get_page_ID_by_page_template($template_name)
{
global $wpdb;
$page_ID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '$template_name' AND meta_key = '_wp_page_template'");
return $page_ID;
}
/* -- Get page ID by Custom Field Value -- */
function get_page_ID_by_custom_field_value($custom_field, $value)
{
global $wpdb;
$page_ID = $wpdb->get_var("
SELECT wposts.ID
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = '$custom_field'
AND (wpostmeta.meta_value like '$value,%' OR wpostmeta.meta_value like '%,$value,%' OR wpostmeta.meta_value like '%,$value' OR wpostmeta.meta_value = '$value')
AND wposts.post_status = 'publish'
AND wposts.post_type = 'page'
LIMIT 0, 1");
return $page_ID;
}
I need help in rewriting the query below to accept a second and a second + third qualification.
Example meta keys and values:
Trees
meta_key = trees
meta_value = pine, oak, sequoia
States
meta_key = states
meta_value = california, washington, florida
Countries
meta_key = countries
meta_value = usa, uk, ireland
I attempted to add these AND statements but I was not able to cause it to work properly.
Second qualification:
AND (pm.meta_key = 'states' AND pm.meta_value = 'california')
Second and third qualification:
AND (pm.meta_key = 'states' AND pm.meta_value = 'california')
AND (pm.meta_key = 'countires' AND pm.meta_value = 'usa')
Function with query I need help in rewritting:
function get_unique_post_meta_values( $key = 'trees', $type = 'post', $status = 'publish' ) {
global $wpdb;
if( empty( $key ) )
return;
$res = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $res;
}
Thanks in advance for your help! Please let me know if further clarification is required.
If the function sets $key=trees, what is the point of an empty check? Additionally, I think you are missing the curly brackets. You are selecting 'pm.meta_value', which is why are you doing a LEFT JOIN, I think? Maybe seeing your DB structure would help me understand that, but an INNER JOIN is what you want.
if( empty( $key ) ){
return;
} else {
$res = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} as p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $res;
}
Try this. If it doesn't work then update this question with more details and I will help guide you further.
EDIT: I elaborated more on this topic in another thread: List categories by author ~ WITH COUNTER ~ (Wordpress) ... it might be more suitable for you.
When I'm in my Category page I want the readers to see who's posting there, and filter by Author. (Ex. Activism has 127 post, 37 by Adrian, 12 by James, etc…) I've tried a million things out there, but nothing serves my simple task! Anyone with similar experience out there?
This is what I've got:
<?php
$curauth = $wp_query->get_queried_object();
$auth_id = 1;
$cat_id = 2411;
$sql = "SELECT COUNT(*)
FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
WHERE post_author = $auth_id
AND post_type = 'post'
AND post_status = 'publish'
AND post_date <= NOW()
AND tr.term_taxonomy_id = $cat_id
";
$post_count = $wpdb->get_var($sql);
echo "This Author has published $post_count posts in Activism<br />";
?>
EDITED:
OK, this code works!!! ... I'm happy, but still a bit puzzled. Why does this work, and the pne above not?
<?php
$auth_id = 1;
$cat_id = 2411;
$counter = "SELECT COUNT(*)
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = $cat_id
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND post_author = '$auth_id'
";
$user_count = $wpdb->get_var($counter);
echo "Author... has $user_count posts in Activism<br />";
?>