add_action('transition_post_status', 'send_new_post', 9876543210, 3);
function send_new_post($new_status, $old_status, $post) {
if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'product') {
global $wpdb;
$current_product_id = $post->ID;
if(isset($current_product_id)) {
$post_id = $current_product_id;
} else {
$post_id = '131';
}
$sql = "SELECT
post_id as id,
(SELECT post_title FROM wp_posts WHERE id = pm.post_id) AS title,
(SELECT post_name FROM wp_posts WHERE id = pm.post_id) AS name,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_price' LIMIT 1) AS price,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_regular_price' LIMIT 1) AS 'regular_price',
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_stock' LIMIT 1) AS stock,
IFNULL((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_sku' LIMIT 1),
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_custom_field' LIMIT 1)) as sku
FROM `wp_postmeta` AS pm
JOIN wp_posts AS p ON p.ID = pm.post_id
WHERE p.ID = ".$post_id." AND meta_key in ('_product_version')
AND p.post_status in ('publish')";
$results = $wpdb->get_results($sql);
$result = json_decode(json_encode($results), true);
$data_init = $result[0];
$tablename=$wpdb->prefix.'product_init';
$data=array(
'post_id' => $data_init['id'],
'post_title' => $data_init['title'],
'post_name' => $data_init['name'],
'price' => $data_init['price'],
'regular_price' => $data_init['regular_price'],
'sku' => $data_init['sku'],
'stock' => $data_init['stock'],
'created_by' => 'Created By Custom Code'
);
$wpdb->insert( $tablename, $data);
}
}
Somehow i only get this
I can understand by i am not getting other values price, regular_price, sku, and stock ..........
Related
I am trying to delete WordPress post after X Days.
And i am using this code.
$daystogo = 30;
$sql =
"UPDATE {$wpdb->posts}
SET post_status = 'trash'
WHERE (post_type = 'post' AND post_status = 'publish')
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query($wpdb->prepare( $sql, $daystogo ));
But i want to exclude some post on meta key.
Like i don't want to delete that post which have FEATURD POST VALUE IS 1
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
)
),
Any way to add this condition in query??
Thanks
Here is mysql code for you, you can use MySQL "IN()" function for this:
$sql =
"UPDATE {$wpdb->posts}
SET post_status = 'trash'
WHERE (post_type = 'post' AND post_status = 'publish')
AND ID not in (select post_id from {$wpdb->postmeta} where
meta_key='featured_post' and meta_value='1' )
AND DATEDIFF(NOW(), post_date) > %d";
I'm kind of venturing into uncharted territories with this one. Is there any way to get a query of all WooCommerce orders (from all users)? And have it displayed in a table, similar to how it is in the Admin 'Orders' page? I tried to study how the Admin 'Orders' page works but that was actually very unsuccessful.
Any help at all would be greatly appreciated!
I think , you want all orders list with product details , and customer details etc , So you have to create own custom Query to get the result to display in the report .
As below query i also used for my project.
So it will be helpful to you .
SELECT
p.ID as order_id,
p.post_date,
max( CASE WHEN pm.meta_key = '_billing_email' AND p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
max( CASE WHEN pm.meta_key = '_billing_postcode' AND p.ID = pm.post_id THEN pm.meta_value END ) as _billing_postcode,
max( CASE WHEN pm.meta_key = '_shipping_first_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_first_name,
max( CASE WHEN pm.meta_key = '_shipping_last_name' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_last_name,
max( CASE WHEN pm.meta_key = '_shipping_address_1' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_1,
max( CASE WHEN pm.meta_key = '_shipping_address_2' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_2,
max( CASE WHEN pm.meta_key = '_shipping_city' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_city,
max( CASE WHEN pm.meta_key = '_shipping_state' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_state,
max( CASE WHEN pm.meta_key = '_shipping_postcode' AND p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_postcode,
max( CASE WHEN pm.meta_key = '_order_total' AND p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
max( CASE WHEN pm.meta_key = '_order_tax' AND p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
max( CASE WHEN pm.meta_key = '_paid_date' AND p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
( SELECT GROUP_CONCAT( order_item_name separator '|' ) FROM wp_woocommerce_order_items WHERE order_id = p.ID ) as order_items
FROM
wp_posts p
JOIN wp_postmeta pm on p.ID = pm.post_id
JOIN wp_woocommerce_order_items oi on p.ID = oi.order_id
WHERE
post_type = 'shop_order' AND
post_status = 'wc-completed'
GROUP BY
p.ID
I think that a good start should be to build a WP_query with the following args:
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status', 'field' => 'slug',
'terms' => array('processing')
)
)
);
Here's how I would start. But, I don't have a deep enough understanding of WP_List_Table right now to fill it out.
This needs to be in a plugin:
function orders_redux_menu(){
if ( current_user_can( 'manage_woocommerce' ) ) {
add_submenu_page( 'woocommerce', __( 'Orders Part Deux', 'your-plugin-textdomain' ), __( 'Orders Part Deux', 'your-plugin-textdomain' ) , 'manage_woocommerce', 'wc-orders-redux', 'orders_redux_page' );
}
}
add_action( 'admin_menu', 'orders_redux_menu', 15 );
function orders_redux_page() {
if ( ! class_exists( 'WP_Posts_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php' );
}
include_once('orders-table-redux.php');
$new_table = new Orders_Redux_List();
?>
<div class="wrap">
<h2><?php _e( "Orders Redux", "your-plugin-textdomain" );?></h2>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form method="post">
<?php
$new_table->prepare_items();
$new_table->display(); ?>
</form>
</div>
</div>
</div>
<br class="clear">
</div>
</div>
<?php
}
And then in another file in the plugin root called orders-table-redux.php
<?php
class Orders_Redux_List extends WP_Posts_List_Table {
static $post_type = 'shop_order';
/** Class constructor */
public function __construct() {
//$screen = convert_to_screen( 'wc-orders-redux' );
parent::__construct( [
'singular' => __( 'Order Redux', 'sp' ), //singular name of the listed records
'plural' => __( 'Orders Redux', 'sp' ), //plural name of the listed records
'ajax' => false, //should this table support ajax?
'screen' => 'edit-' . self::$post_type // this doesn't do what I'd hoped yet
] );
}
}
WP_List_Table has a lot of methods you can override to customize the columns, the results, etc.
Here's an example tutorial of a custom table implementation: https://www.sitepoint.com/using-wp_list_table-to-create-wordpress-admin-tables/
I filtered the post's in my sidebar by using this code,
$mostlikequerystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->postmeta.meta_value DESC
LIMIT 0 , 10";
This code working perfect , but now i want to add a category filter too..
for this i used $term_id
global $wpdb;
$term_id = get_term_by('slug','trailers');
$term_id->term_id;
echo $term_id;//Prints 12
$mostlikequerystr="SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish')
LIMIT 0 , 10";
$tariler_post = $wpdb->get_results($mostlikequerystr, 'OBJECT');
echo $wpdb->show_errors();
but its not working for me and no error too ...
You are missing with conditional operator in your WHERE clause
WHERE ($wpdb->term_taxonomy.term_id = $term_id here
^^^^^
$wpdb->posts.ID = $wpdb->postmeta.post_id
Try this one by adding AND
$mostlikequerystr = "
SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->postmeta.meta_value DESC
)
LIMIT 0 , 10";
If you are using WPDB class try to catch the errors
<?php $wpdb->show_errors(); ?>
Other way you can use WP's built in functions
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'most_liked',
'value' => '',
'compare' => '!='
),
'category__in' => 'id here or skip this argument'
'orderby' => 'meta_value_num',
);
$tariler_post=new WP_Query($args);
This is my query. So i need to retrieve the sum from the values of budget meta key which are only in component 1 and in year 2009. How do i do this. Thanks in advance
SELECT SUM(
b.meta_value)
, b.meta_key
FROM wp_posts AS p, wp_postmeta AS b, wp_postmeta AS m, wp_postmeta AS n
WHERE (
p.ID = b.post_id
)
AND (
b.meta_key = 'budget'
)
AND (
m.meta_key = 'component'
AND m.meta_value = '1'
)
AND (
n.meta_key = 'component-year'
AND n.meta_value = '2009'
)
OK. So I think what you are saying is that you have posts with three custom fields; "component", "component-year", and "budget". You are trying to get a total of values of "budget" when component=1 and component-year=2009. If that is correct, then this works - it is not the most efficient but it works fine.
SELECT SUM(wp_postmeta.meta_value) AS total
FROM wp_posts
LEFT JOIN wp_postmeta ON (
wp_posts.ID = wp_postmeta.post_id
AND
wp_postmeta.meta_key = 'budget'
)
# get all the posts with a custom field of "component" of "1"
WHERE wp_posts.ID IN (
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'component'
AND meta_value = '1'
)
# and all the posts with a custom field of "component-year" of "2009"
AND wp_posts.ID IN (
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'component-year'
AND meta_value = '2009'
);
I would like to get ordered all my posts with multiple cutom fields (Wordpress 3).
Exemple, I've 2 custom couple meta_key/meta_value :
" order_submenuexpositions / numeric "
" display_submenuexpositions / boolean "
I tried like this, but the result it's not orderer by meta_value from "order_submenuexpositions" meta_key :
SELECT * FROM wp_posts
LEFT JOIN wp_postmeta wpostmetaOrder ON ( wp_posts.ID = wpostmetaOrder.post_id AND wpostmetaOrder.meta_key = 'order_submenuexpositions' )
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
LEFT JOIN wp_term_relationships wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_postmeta.meta_key = 'display_submenuexpositions'
AND wp_postmeta.meta_value = 'true'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
ORDER BY wpostmetaOrder.meta_value ASC,wp_postmeta.meta_value ASC
how can i do it ?
thank you !
ok resolved, it's because meta_value sql field type is longtext and i tried to order on integer value..it's doesn't work.you have to cast type on integer, like this :
SELECT * FROM wp_posts
LEFT JOIN wp_postmeta wpostmetaOrder ON ( wp_posts.ID = wpostmetaOrder.post_id AND wpostmetaOrder.meta_key = 'order_submenuexpositions' )
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
LEFT JOIN wp_term_relationships wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_postmeta.meta_key = 'display_submenuexpositions'
AND wp_postmeta.meta_value = 'true'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
ORDER BY CAST(wpostmetaOrder.meta_value AS SIGNED) ASC,wp_postmeta.meta_value ASC`
You can actually do this without using a complex SQL query for future reference.
Here is an Example:
$args = array(
'posts_per_page' => 250,
'post_type' => 'books',
'meta_query' => array(
array(
'key' => 'author',
'value' => $author_name,
'compare' => '=='
),
array(
'key' => 'publisher_id',
'value' => $publisher_id,
'compare' => '!=',
'type' => 'NUMBER'
)
)
);
$the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) : $the_query->the_post();
// LOOP GOES HERE
endwhile;
You can add additional arrays inside "Meta_query" to dig even deeper and you can also learn about the different types of 'compare' and 'type' options available at the wordpress codex.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters