I added a line in my Wordpress HTML code ( header.php ) to set the date of last update. You understand that i have to make it everytime i make a new post or put a new product. Is there a way to do it automatically? I'd like to get the date of the last post/product and echo it in the header. My knowledge of PHP is poor, sorry for that.
Add this code in functions.php
<?php
function last_updated(){
global $wpdb;
$sql = "SELECT post_modified
FROM $wpdb->posts
WHERE post_type='post' OR post_type='product'
AND post_status='publish'
ORDER BY post_modified DESC
LIMIT 1";
$last_update = $wpdb->get_var( $sql );
$last_update = date("d-m-Y", strtotime($last_update));
return $last_update;
}
add_shortcode('last_updated_date','last_updated');
?>
Add this code in header.php
<?php echo do_shortcode('[last_updated_date]');?>
Related
Two questions here.
Below is a function in WooCommerce wc-order-functions.php that I found works almost similar to what I need.
function wc_get_order_id_by_order_key( $order_key ) {
global $wpdb;
// Faster than get_posts()
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND meta_value = %s", $order_key ) );
return $order_id;
}
Which returns the $order_id but I'm looking to get the $order_key so that I can contruct the url generated after buyer have click the 'Place order' button on the checkout page e.g. domain.com/checkout/order-received/2316/?key=wc_order_54c7142660e24
wc_order_54c7142660e24 being the $order_id and
2316 being the $order_key
I know that the SQL command below gets the value I want from database:
function wc_get_order_key_by_order_id( $order_id ) {
global $wpdb;
// Faster than get_posts()
$order_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND post_id = %s", $order_id ) );
return $order_key;
}
But I'm not sure how to hook the filter to wordpress functions.php So this is my first question.
Second question is I've been searching for the function that might be responsible for generating the meta_value for the $order_key but can't seem to find it, would be great to know how it works. I suppose the first way is kinda redundant since the function I'm looking for already exists, just can't seem to locate it.
Try adding this to your woocommerce template files where you need that link:
echo get_site_url().'/checkout/order-received/'.$order->id.'&key='.$order->order_key;
I'm returning some data from custom table, but I'm not sure if is the code enough secured. Can you help me?
$page_title = get_the_title();
$id = $wpdb->get_row('select id from table where name="'.htmlspecialchars($page_title).'"');
Not sure why you would match a title, usually when doing a search its better to use an id.
But this is what a custom query would look like to get a post id when the title matches the var. prepare does the proper escaping. See the first place prepare comes up here http://codex.wordpress.org/Class_Reference/wpdb
global $wpdb;
$page_title = get_the_title();
$id = $wpdb->get_row($wpdb->prepare('SELECT id FROM ' . $wpdb->posts . ' WHERE post_title = %s', $post_title));
Or you could use WordPress's esc_sql() function on your var.
http://codex.wordpress.org/Function_Reference/esc_sql
But like I said, this query seems backwards. If you have get_the_title() you should also have the_ID() or get_the_ID() so the query is not needed. So it depends what you are trying to do.
I am trying to query the wordpress post_meta table by meta_value.
I would like to output all post_id's where the meta_value is = to _parent_product. Here is my code:
$posts = $wpdb->get_results("SELECT *, FROM $table WHERE meta_key='_parent_product' ");
foreach ( $posts as $post ){
$id = $post->post_id;
echo $id;
}
The above outputs nothing and im not quite sure why? Can anyone see anything wrong?
As stated in the comment,
There is a comma (,) behind SELECT *,. Therefor the given SQL is invalid and will fail to retrieve any results.
Can anybody point me in the right direction? I'm trying to get pagination to work because I just have too many entries on the page (see link to sample page below), but am completely in the weeds.
This is for an archive page which lists custom posts based on a custom field (start date) and also drops them from the list when the start date passes.
Here's a sample page:
http://www.musicfestivaljunkies.com/festival-guide/us-festivals/
Here's the code I've been working with:
<?php
/**
* Do we need to filter by event tag?
*/
if(is_tax('event_tags') ) :
$tag = strip_tags( get_query_var('event_tags') );
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->terms wterms, $wpdb->term_taxonomy wtax, $wpdb->term_relationships wrels
WHERE wposts.ID = wpostmeta.post_id
AND wterms.term_id = wtax.term_id
AND wtax.term_taxonomy_id = wrels.term_taxonomy_id
AND wrels.object_id = wposts.ID
AND wterms.slug = '$tag'
";
else:
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id";
endif;
// Build the rest of the query, i.e. only get events with dates, and order newest first.
$querystr .= "
AND wpostmeta.meta_key = 'Date'
AND STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') ASC
";
$events = $wpdb->get_results($querystr, OBJECT);
if ($events):
echo '<ul>';
foreach ($events as $post):
global $post;
setup_postdata($post);
// Get a friendlier version of the date.
$date = get_post_meta($post->ID, 'Date', true);
$date = date_create($date);
$date = date_format($date, 'jS F, Y');
?>
<li><?php the_title(); ?> - <?php echo $date; ?></li>
<?php endforeach;
echo '</ul>';
endif; ?>
Unless you're trying to get some seriously unique data, writing your own query is usually something to avoid in WordPress. In this case, you'll want to look into the get_posts() method ( http://codex.wordpress.org/Template_Tags/get_posts ). I'm not 100% sure exactly which parameters you'll need to accomplish your goals, but definitely start by looking at the 'post_type' argument, as well as 'meta_key' and 'meta_value', which you can use to find posts within a particular custom type that have a particular meta value set on them (it appears that's at least partially what you're looking to accomplish).
If this doesn't get you headed in the right direction, please let me know what you're trying to accomplish a little more specifically and maybe I can clarify further.
UPDATE: This section of the get_posts() doc should help specifically with custom fields, and as a bonus it's using a custom post_type as well: http://codex.wordpress.org/Template_Tags/get_posts#Custom_Field_Parameters
I built a very unique and javascript intensive theme for wordpress and now shortcodes do not work. I do not have any plugins installed, so it's not that. What did I drop from my wordpress template files that is required to use shortcodes (ie: [gallery]).
I understand how to make shortcodes, but how does WP take your post and replace "[gallery]" when it is spitting it back out for display?
EDIT:
here is what I'm currently working with:
$pagepull = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", ARRAY_A);
$i = 1;
foreach ($pagepull as $single_page){
echo "<div class=\"section\"><ul><li class=\"sub\" id=\"" . $i . "\"><div class=\"insection\">";
echo $single_page['post_content'];
$i++;
// more code that is irrelevant...
// more code that is irrelevant...
// more code that is irrelevant...
}
Ok, try this
$pagepull = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", ARRAY_A);
$i = 1;
foreach ($pagepull as $single_page){
echo "<div class=\"section\"><ul><li class=\"sub\" id=\"" . $i . "\"><div class=\"insection\">";
echo apply_filters('the_content',$single_page['post_content']);
$i++;
Wordpress take your content and apply filters to it. You must register a filter and let parse your content.
If your theme is not displaying your shortcodes, probabily you output the content of the post without let Wordpress filter it.
Calling the function get_the_content() for a post, does not run the filter for shortcodes (if any).
To have apply
<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>
Ref: http://codex.wordpress.org/Function_Reference/get_the_content
Note: many plugins register filters with the content to implement shortcodes!
My solution was replacing
<?= get_the_content() ?>
with
<?= the_content() ?>
which, as keatch already mentioned, applies filters before returning content.
Read this carefully about the_content
use this if you want the content inside a variable:
ob_start();
the_content();
$content = ob_get_clean();
now you could just do echo $content; or use regex or whatever you want to make the content look like how you want it.
I had the same issue.
Shortcodes depends on WP Loop, but that's a different issue. To make a long story short, I've added the_post(); at the page that should be showing the shortcode (for example articles.php).
Also, make sure that you are using the_content() in order to display the text (using $post->post_data for example won't show you shortcodes).
Please use the
ob_start();
in the starting of function and use
return ob_get_clean();
before closing the function.
Hope this will help full for you.
Cheers