Get three posts before a certain date in Wordpress - wordpress

I was wondering how I can get the last three posts before (and after) a certain date.
I was looking at query_post but I can't figure it out.
I'm looking to do this in functions.php. Ideally, it would return just basic home page stuff.. title, first image, num_comments, etc. I don't need to query the whole article.
EDIT: I would also like this all done in one function.. there are explanations out there on how to add a filter function.. which I don't want to do. If the functionality could be simply placed in add_filter(...) that would be fine.
Any help would be very much appreciated!
Matt

Got'er. Uses AJAX. Thank you for being a baller PHP (inner functions).
function ajax_get_next_posts($wp) {
if(!$_POST['date']) return 'error';
function filter_where($where = '') {
$where .= " AND post_date <= '{$_POST['date']}'";
return $where;
}
add_filter('posts_where', 'filter_where');
$posts = query_posts('posts_per_page=3');
print_r($posts);
}

Related

Custom Order Listing Page With WooCommerce

I have created a plugin which has a separate page for order listing.. in that it looks like the same as WooCommerce's Order Listing page but. i am unable to get the comments of the order so i added my custom post type to wc_order_types after that there is no order listed.. its showing a empty table. ?
add_filter( 'wc_order_types',array($this,'add_wc_order_types'),10,3);
public function add_wc_order_types($order_types,$type){
$order_types[] = WC_QD_PT;
return $order_types;
}
apply_filters( 'wc_order_types', $order_types, $for ); is default wc_filters which takes 2 parameters you have asked for 3 here add_filter( 'wc_order_types',array($this,'add_wc_order_types'),10,3); and again supplied 2.
visit http://docs.woothemes.com/wc-apidocs/source-function-wc_get_order_types.html#149 It may help you do this.
I Solved The issue just by adding a if condition in my hook's function
function add_wc_order_types($order_types,$type){
$order_type = $order_types;
if('' == $type){
$order_type[] = WC_QD_PT;
}
return $order_type;
}

Get all product variations of a product given its ID in Woocommerce

I have a custom page where I'm trying to list every products in the store along with their variations.
Also, I'm trying to list the variations' prices sorted by the product attribute with slug 'size'
For testing, I'm trying to get the variations of a single product with the ID 381
My code yet is
$handle=new WC_Product('381');
$variations1=$handle->get_avaialable_variations();
foreach ($variations1 as $key => $value) {
echo '<option value="'.$value['variation_id'].'">'.implode('/',$value['attributes']).'-'.$value['price_html'].'</option>';
}
But the error I'm getting is
PHP Fatal error: Call to undefined method WC_Product::get_avaialable_variations()
I tried using
$handle=new WC_Product_Variable('381');
instead of
$handle=new WC_Product('381');
But the error is the same.
Any help here?
Try this code.
$handle=new WC_Product_Variable('12');
$variations1=$handle->get_children();
foreach ($variations1 as $value) {
$single_variation=new WC_Product_Variation($value);
echo '<option value="'.$value.'">'.implode(" / ", $single_variation->get_variation_attributes()).'-'.get_woocommerce_currency_symbol().$single_variation->price.'</option>';
}
Note: Use this $single_variation->get_price_html() but its outputs with html span tag which results in getting hidden in option tags.
Tested the above code and the results are as follows.
Let me know if that worked for you too.
You had a typo in your code - get_avaialable_variations
It should be get_available_variations
function get_variation_data_from_variation_id($item_id) {
$_product = new WC_Product_Variation($item_id);
$variation_data = $_product->get_variation_attributes(); // variation data in array
$variation_detail = woocommerce_get_formatted_variation($variation_data, true); // this will give all variation detail in one line
// $variation_detail = woocommerce_get_formatted_variation( $variation_data, false); // this will give all variation detail one by one
return $variation_data; // $variation_detail will return string containing variation detail which can be used to print on website
// return $variation_data; // $variation_data will return only the data which can be used to store variation data
}
I found this before on stackoverflow couldn't remember the link (please edit if find the link to the answer with this method). it shows the variation data for output. the method takes variation id as perameter.
output:
.

strpos() on title in a metabox in wordpress fails to find '-'

I was coding a simple metabox for wordpress and have a little issue when saving data.
The meta box is in my "create article"-page has two textfields. These are saved as post-meta, when the post is saved.
While saving I check if the fields were filled - if they are empty I take the post title and extract the data I need. The idea is to take everything that is before the first "-". If there is no minus sign, the whole title should be saved in my custom field. Now, this fails to find "-" in the title (alltough there is one) and returns the whole title every time:
function get_from_title($title) {
$pos = strpos($title, '-');
if ($pos) {
return trim(substr($title, $pos));
}
else {
$pos = strpos($title, '–'); //added this since two different signs could be used
if ($pos) {
return trim(substr($title, $pos));
}
else {
return $title;
}
}
}
the function that is calling get_from_title is getting the title via get_the_title( $post_id ) and this works without problems.
Is wordpress encoding the title somehow? Why can't strpos find the minus sign? What should I look for instead?
Thanks
I replied to the thread you started on this topic in the WordPress forums. You can find your answer there.
Alternatively, here's what I said. :)
Ah yes. This is a tricky one. So, why can't strpos find a hyphen in
the title when clearly we can see one? Because there isn't one. hehe.
What WordPress is doing here is converting your hyphen ( minus sign )
into an en-dash.
This will give you diddly-squat:
$pos = strpos( $title, '-' );
You want this:
$pos = strpos( $title, '–' );
Let me know how things turned out for you. :)

Wordpress: Sort posts chronologically by custom field

I have some posts used as events, with a custom field for the event's date. I'm looking for ordering thoses posts by event's date (chronologically).
I'm wondering:
Is it possible to set the event's date custom field as a "date" format
(for now, it's a simple type=text).
Howto order thoses posts if i always
use the same format (like
dd/mm/yyyy). I need to get the 3 close coming events (not the last 3).
Did wordpress has a built-in function who could compare a date to the today's date?
thanks
I just did this! (www.wherewordsgo.com)
okay, i only got it working for 'pages' but.
but if you make a meta box for 'deadline' and then put the date in yyyy/mm/dd format and add this to your functions.php it might help:
add_action('wp', 'check_page');
function check_page () {
if (is_page()) {
add_filter('get_previous_post_sort', 'sort_it');
add_filter('get_next_post_sort', 'sort_it');
add_filter('posts_orderby', 'sort_it' );
add_filter('posts_join', 'join_it' );
add_filter('posts_where', 'where_it' );
function sort_it () {
global $wpdb;
return " $wpdb->postmeta.meta_value ASC ";
}
function join_it( $join ) {
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
return $join;
}
function where_it( $where ) {
global $wpdb;
$where .= "AND $wpdb->postmeta.meta_key = 'deadline' ";
return $where;
}
}
I got that from some site (which I have lost the link for... big WP dude though I think) and changed it as needed. Like I said it works only for custom pages on my build (I'm using a custom type), but may well just work for everything if you change the 'is_page()' function at the start to whatever is relative for you.
Hope that helps

single hook_form for many form ids

in my module i am creating a page with many simple forms:
foreach ($parameters as $param){
$output = drupal_get_form('mymodule_param_'.$param['id'].'_form', $product);
}
print $output;
i need to find a way to implement a single mymodule_param_form function (and the _validate , _submit ) that will apply on each $param['id'] in my $params array.
i trust drupal to allow such behavior. just didnt find it yet.
any help ?
You practically posted your own answer: hook_forms

Resources