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;
}
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;
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');
I want a select box that allows users to pick the order of posts displayed. (Like youtube order by date / relevance)
I'm displaying a list of custom post types (products). I want users to be able to select the order by price & size. (these are both custom fields).
I've written this code which allows me to change the order by changing the default variables below.
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $wpdb->postmeta.meta_value $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
I now need to let public change the default variables with a select box, but I don't know how.
<form method="post">
<select name="custom_field_choice">
<option value="size">Size</option>
<option value="price">Price</option>
</select>
<input type="submit" value="order_select" />
</form>
Is it possible to allow a user to change a php variable with a select box? If not, whats the best way to do this?
Try to change your code to receive variable from http post request like this
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
$orderField = $post->custom_field_choice;
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $orderField $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
Option's values size and price must be column name on your database table.
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 />";
?>
Im trying to write a query that will find and display all of my posts that have the same custom field values as my input.
In wordpress I have the following...
My query is...
$pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = 'petrol' OR wpostmeta.meta_value = 'local' ORDER BY wpostmeta.meta_value DESC", OBJECT);
If I remove 'OR wpostmeta.meta_value = 'local' This works correctly and pulls the correct post with the custom field value as 'petrol'
Can anybody give me an idea on where im going wrong? At the moment its display all of my posts even those that are drafts and have been deleted and its also looping and displaying them numerous times...
Try:
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND (wpostmeta.meta_value = 'petrol' OR wpostmeta.meta_value = 'local')
ORDER BY wpostmeta.meta_value DESC"
Figured it out...
<?php
$customkey = 'Network'; // set to your custom key
$customvalue = 'Local'; // set to custom value
global $wpdb;
$my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE ID = $wpdb->postmeta.post_id AND meta_key = '$customkey' AND meta_value = '$customvalue' ORDER BY post_date DESC");
foreach ($my_posts as $post) :
setup_postdata($post);
echo '<div><a href="';
the_permalink();
echo '"></div>';
the_title();
endforeach;
?>