Search for lack of ID in two WOOCOMMERCE tables

Asked

Viewed 16 times

1

I need to search and return todos os pedidos who are in status processando, and who possess the column meta_value in vazio or inexistente in another table.

1º - In wordpress, I will check all requests made in the table wp_posts.

2º - In the same table wp_posts, I need to filter all orders that are with the status wc-processing in the column post_status.

3º - Then I get the table Ids wp_posts, and see which ones don’t exist on the table wp_postmeta that has as ID the column post_id.

4º - The column post_id table wp_postmeta, may have multiple lines with the same ID, but I only have to filter those that have the column meta_key = _correios_tracking_code.

5th - Finally, I must return all posts, which do not have the meta_key = _correios_tracking_code, plus the posts that have the meta_key = _correios_tracking_code, but are empty.

SQL:

  Tabela wp_posts
    _________________________
    |  ID  |  post_status  |
    _________________________
    |  1   | wc-processing |
    -------------------------
    |  2   | wc-processing |
    -------------------------
    |  3   |    wc-fail    |
    -------------------------
    |  4   | wc-processing |
    _________________________


Tabela wp_postmeta
_______________________________________________________
|  post_id |          meta_key       |   meta_value   |
_______________________________________________________
|     1    | _correios_tracking_code |  PM353535353BR |
-------------------------------------------------------
|     1    |          _codex         |      Elite     |  
-------------------------------------------------------
|     2    | _correios_tracking_code |                |
-------------------------------------------------------
|     2    |          _codex         |      Elite     |  
-------------------------------------------------------

Wordpress:

add_filter( 'views_edit-shop_order', array( $this, 'shop_order_filters' ), 10, 1 );

public function shop_order_filters( $views ) {
        global $wpdb;

        $sql = "SELECT
          (DISTINCT p.ID)
        FROM
          $wpdb->posts p
        WHERE
          p.post_status LIKE 'wc-processing'
          AND p.post_type = 'shop_order'";
          

        $v= $wpdb->get_var($sql);
        $views = //FILTROS
        return $views;
  }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.