Printing all WooCommerce Orders (for admin users) - php

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/

Related

maatwebsite/excel Export Multiple Sheet in Laravel

i am using WithMultipleSheets and FromView for exporting excel sheet
worksheet 1 have a sales order and worksheet 2 have orders list
sales order records are fetch correctly but orders record are fetch less then 35
for example original record is 85 but in sheet only 25 exported
here is my code for reference
app/exports/reportexcel.php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Excel;
class ReportExport_Multiple implements WithMultipleSheets
{
public function sheets(): array
{
if (session_status() === PHP_SESSION_NONE) { session_start(); }
return [
new SalesReportExport_Multiple(),
new OrderReportExport_Multiple(),
];
}
}
app/exports/Orderreport.php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Excel;
use DB;
class OrderReportExport_Multiple implements FromView
{
public function view(): View
{
$OrderData = DB::connection('mysql2')->select("
SELECT oi.order_id,o.post_date,oi.order_item_id,oi.order_item_name,
CASE o.post_status
WHEN 'wc-processing' THEN 'Processing'
WHEN 'wc-completed' THEN 'Completed'
ELSE 'Unknown'
END AS 'PurchaseStatus',
MAX( CASE WHEN pm.meta_key = '_order_total' AND o.ID = pm.post_id THEN pm.meta_value END ) AS 'OrderTotal',
MAX( CASE WHEN pm.meta_key = '_payment_method' AND o.ID = pm.post_id THEN pm.meta_value END ) AS 'PaymentMethod',
MAX( CASE WHEN pm.meta_key = '_paid_date' AND o.ID = pm.post_id THEN pm.meta_value END ) AS 'PaidDate',
MAX( CASE WHEN pm.meta_key = '_billing_first_name' and o.ID = pm.post_id THEN pm.meta_value END ) as FirstName,
MAX( CASE WHEN pm.meta_key = '_billing_last_name' and o.ID = pm.post_id THEN pm.meta_value END ) as LastName,
MAX( CASE WHEN pm.meta_key = '_billing_email' and o.ID = pm.post_id THEN pm.meta_value END ) as StudentEmail,
MAX( CASE WHEN pm.meta_key = '_billing_phone' and o.ID = pm.post_id THEN pm.meta_value END ) as StudentPhone,
( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = '_qty'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'Qty',
( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = '_line_total'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'LineTotal'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'size'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'Sizes'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'back_name'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'BackName'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'nick_name'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'NickName'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'front_name'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'FrontName'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'back-number'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'BackNumber'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'name'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'Name'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'number'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'Number'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'namenotreq'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'NameNotReq'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'numbernotreq'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'NumberNotReq'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'select_embroidery'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'SelectEmbroidery'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'select_thermo_flim'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'SelectThermoFlim'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'back_name_not_req'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'BackNameNotReq'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'front_name_not_req'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'FrontNameNotReq'
, ( SELECT GROUP_CONCAT(CONCAT( IF(m.meta_value IS NOT NULL,m.meta_value,'---') ))
FROM wp_3_woocommerce_order_items i
LEFT JOIN wp_3_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND meta_key = 'select_back_text'
WHERE i.order_id = o.ID AND i.order_item_type = 'line_item') AS 'BackText'
FROM wp_3_term_relationships tr
INNER JOIN wp_3_term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_3_terms t
ON tt.term_id = t.term_id
INNER JOIN wp_3_woocommerce_order_itemmeta oim
ON tr.object_id = oim.meta_value
INNER JOIN wp_3_woocommerce_order_items oi
ON oim.order_item_id = oi.order_item_id
INNER JOIN wp_3_posts as o
ON oi.order_id = o.ID
INNER JOIN wp_3_postmeta AS pm ON o.ID = pm.post_id
WHERE tt.taxonomy = 'product_cat'
".$allOrders."
AND oim.meta_key = '_product_id'
AND o.post_type = 'shop_order'
AND o.post_status IN ('wc-completed','wc-processing')
AND o.post_date BETWEEN '".$startDate."' AND '".$endDate."'
GROUP BY oim.meta_id Order By oi.order_id DESC, oi.order_item_id ASC
");
return view('ExportData.OrderReportExport',[ 'OrderData' => $OrderData]);
}
}
resource/view/exportdata/orderexport.blade.php
foreach ($OrderData as $key => $OrdersData) { table body for display record in excel sheet }
query return 85 data
but in excel show only 25
only issue in orders list sales list are correct
need to store all data which fetch from query
run excel export from here
Excel::store(new ReportExport_Multiple, $filePath, 's3');
please help to for issue

Add product details when publish a product (WordPress)?

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 ..........

wordpress meta query always fails even if data is correct

I've following query -
$query = array(
'post_type' => 'accessory',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'accessory-bike',
'value' => array("33"),
'compare' => 'IN'
)
)
);
And now I've this record in database -
520 is the ID for accessory post type. It still returns 0 results. I can't figure where the hell did I do wrong ? Stupid wordpress.
SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND wp_posts.post_type = 'accessory'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')
AND ( ( wp_postmeta.meta_key = 'accessory-bike' AND CAST(wp_postmeta.meta_value AS SIGNED) IN ('33') ) )
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
The value of accessory-bike on post 520 isn't 33; it's a serialized string.
I can see you're using Advanced Custom Fields so all you need to do is pick a different field type that doesn't save data in a serialized string. You may find that something like a custom taxonomy is more appropriate.

How to Add Category Filter in WordPress Post Query

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);

Wordpress query order posts by meta key value

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

Categories