如何使用数据库前台短码WordPress搜索订单

时间:2021-07-22 作者:tyruei

我没有太多的编码知识。如何使用元值将WordPress搜索数据库快捷码添加到前端,就像我要搜索;演示“;,metavalue(命名为serial)将显示包含单词“的数据库”;演示;。我能做的最远的事情就是显示从选择表中得到的所有值。我不打算使用任何插件。

 function wpb_demo_shortcode() {
global $wpdb;


$results = $wpdb->get_results("
SELECT DISTINCT o.order_id, o.`order_item_name`, om.`meta_value` as \'bcs\', 
(select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
 o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'2-certificate\') as \'certificate\',
 (select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'3-serial\') as \'serial\'
 FROM `wp_woocommerce_order_items` as o,
`wp_woocommerce_order_itemmeta` as om,
`wp_postmeta` as pm
WHERE
o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
om.`meta_key` = \'bcs\'
AND 
(select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
 o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'2-certificate\') IS NOT null
 AND  
(select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'3-serial\') IS NOT null
");



$serial = (isset( $_GET[\'$serial\'] )) ? sanitize_text_field($_GET[\'$serial\']) : \'\';
?>
<form action="" method="post">
  <label for="fname">Serial number:</label><br>
  <input type="text" id="$serial" name="$serial"><br>
  <input type="submit" value="Submit">
</form>
<?php
$enteredSerial = $_GET[\'$serial\'];

ob_start();
 echo \'<table><tr>\';
    echo \'<th>Order ID</th>\';
    echo \'<th>Product Name</th>\';
    echo \'<th>BCS</th>\';
    echo \'<th>Serial</th>\';
    echo \'<th>Certificate</th>\';
    echo \'</tr>\';

    foreach( $results as $result ){

        // Html display
        echo \'<tr>\';
        echo \'<td>\' . $result->order_id . \'</td>\';
    echo \'<td>\' . $result->order_item_name . \'</td>\';
    echo \'<td>\' . $result->bcs . \'</td>\';
    echo \'<td>\' . $result->serial . \'</td>\';
    echo \'<td>\' . $result->certificate . \'</td>\';
    echo \'</tr>\';
    }

    echo \'</table>\';
}



add_shortcode(\'greeting\', \'wpb_demo_shortcode\');
我已经为元值添加了占位符(如果我是对的话)。下面的图片将显示前端的外观

Frontend

2 个回复
最合适的回答,由SO网友:tyruei 整理而成

结果发现答案比我想象的要简单

function wpb_demo_shortcode() {
global $wpdb;


$results = $wpdb->get_results("
SELECT DISTINCT o.order_id, o.`order_item_name`, om.`meta_value` as \'bcs\', 
(select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
 o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'2-certificate\') as \'certificate\',
 (select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'3-serial\') as \'serial\'
 FROM `wp_woocommerce_order_items` as o,
`wp_woocommerce_order_itemmeta` as om,
`wp_postmeta` as pm
WHERE
o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
om.`meta_key` = \'bcs\'
AND 
(select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
 o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'2-certificate\') IS NOT null
 AND  
(select  pm.`meta_value`
 from 
 `wp_postmeta` as pm
 WHERE
o.order_item_id = om.order_item_id
AND
pm.`post_id` = o.order_id
AND
 pm.`meta_key` = \'3-serial\') IS NOT null
");



$serial = (isset( $_GET[\'$serial\'] )) ? sanitize_text_field($_GET[\'$serial\']) : \'\';

?>
<form action="" method="get">
  <label for="fname">Serial number:</label><br>
  <input type="text" id="$serial" name="$serial" minlength="3"><br>
  <input type="submit" value="Submit">
</form>
<?php
$enteredSerial = $_GET[\'$serial\'];
echo $enteredSerial;

if(!empty($enteredSerial))
{
ob_start();
 echo \'<table><tr>\';
    echo \'<th>Order ID</th>\';
    echo \'<th>Product Name</th>\';
    echo \'<th>BCS</th>\';
    echo \'<th>Serial</th>\';
    echo \'<th>Certificate</th>\';
    echo \'</tr>\';

    foreach( $results as $result ){
        if (strpos($result->serial, $enteredSerial) !== false)
        {// Html display
        echo \'<tr>\';
        echo \'<td>\' . $result->order_id . \'</td>\';
    echo \'<td>\' . $result->order_item_name . \'</td>\';
    echo \'<td>\' . $result->bcs . \'</td>\';
    echo \'<td>\' . $result->serial . \'</td>\';
    echo \'<td>\' . $result->certificate . \'</td>\';
    echo \'</tr>\';
}
    }

    echo \'</table>\';
}


}


add_shortcode(\'greeting\', \'wpb_demo_shortcode\');

SO网友:Anjan

使用下面的代码,它可以帮助您并确保您的sql查询是正确的,然后它将正常工作。

function wpb_demo_shortcode() {
global $wpdb;
$results = $wpdb->get_results( 
            $wpdb->prepare("SELECT DISTINCT o.order_id, o.`order_item_name`, om.`meta_value` 
                as \'bcs\', (select  pm.`meta_value` from `wp_postmeta` 
                as pm WHERE o.order_item_id = om.order_item_id 
                AND pm.`post_id` = o.order_id 
                AND pm.`meta_key` = \'2-certificate\') 
                as \'certificate\', (select  pm.`meta_value` from  `wp_postmeta` 
                as pm WHERE o.order_item_id = om.order_item_id 
                AND pm.`post_id` = o.order_id 
                AND pm.`meta_key` = \'3-serial\') 
                as \'serial\' FROM `wp_woocommerce_order_items` 
                as o, `wp_woocommerce_order_itemmeta` 
                as om, `wp_postmeta` 
                as pm WHERE o.order_item_id = om.order_item_id 
                AND pm.`post_id` = o.order_id
                AND om.`meta_key` = \'bcs\'
                AND (select  pm.`meta_value` from `wp_postmeta` 
                as pm WHERE o.order_item_id = om.order_item_id
                AND pm.`post_id` = o.order_id
                AND pm.`meta_key` = \'2-certificate\') IS NOT null
                AND (select  pm.`meta_value` from `wp_postmeta` as pm WHERE o.order_item_id = om.order_item_id
                AND pm.`post_id` = o.order_id
                AND pm.`meta_key` = \'3-serial\') IS NOT null") 
            );

$serial = (isset( $_GET[\'$serial\'] )) ? sanitize_text_field($_GET[\'$serial\']) : \'\';
?>
<form action="" method="post">
  <label for="fname">Serial number:</label><br>
  <input type="text" id="$serial" name="$serial"><br>
  <input type="submit" value="Submit">
</form>
<?php
$enteredSerial = $_GET[\'$serial\'];

ob_start();
 echo \'<table><tr>\';
    echo \'<th>Order ID</th>\';
    echo \'<th>Product Name</th>\';
    echo \'<th>BCS</th>\';
    echo \'<th>Serial</th>\';
    echo \'<th>Certificate</th>\';
    echo \'</tr>\';

    foreach( $results as $result ){

        // Html display
        echo \'<tr>\';
        echo \'<td>\' . $result->order_id . \'</td>\';
    echo \'<td>\' . $result->order_item_name . \'</td>\';
    echo \'<td>\' . $result->bcs . \'</td>\';
    echo \'<td>\' . $result->serial . \'</td>\';
    echo \'<td>\' . $result->certificate . \'</td>\';
    echo \'</tr>\';
    }

    echo \'</table>\';
}
add_shortcode(\'greeting\', \'wpb_demo_shortcode\');

相关推荐

如何在unction.php中更改图片url?

例如,我的图像url是https://aa.com/wp-content/uploads/2021/07/x-600x600.jpg我想把它改成https://images.s3.us-east-2.amazonaws.com/aa.com/wp-content/uploads/2021/07/x-600x600.jpg或https://images.s3.us-east-2.amazonaws.com/aa.com/wp-content/uploads/2021/07/x-600x600.jpg.web