WordPress.org

Forums

WP_query and MySQL lost connection (7 posts)

  1. MariuszEm
    Member
    Posted 1 year ago #

    Hi there,

    I've encountered filtering problem. What have I done:

    <?php
      $query_array = array('relation' => 'OR');
        array_push($query_array,
        array(
          'key' => 'filter1',
          'value' => 'value1',
          'compare' => 'LIKE'
        ),
        array(
          'key' => 'filter1',
          'value' => 'value2',
          'compare' => 'LIKE'
        ),
        array(
          'key' => 'filter1',
          'value' => 'value3',
          'compare' => 'LIKE'
        )
      );
    
      $args = array(
        'order' => $order_array,
        'meta_key' => $meta_key,
        'orderby'   => $orderby,
        'post_type' => 'page',
        'paged' => $paged,
        'post__in' => $postIDs,
        'posts_per_page' => 12,
        'paged' => get_query_var('paged'),
        'meta_query' => $query_array
      );
    
      query_posts($args);
      ?>

    And here is what I recieve when debug mode is TRUE (wp-config):

    WordPress database error: [Lost connection to MySQL server during query]

    And red colored query:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postme[...]

    What else can I say? When I remove third array, everything works like a charm. It doesn't matter which one. When it is array of 4 (relation=>OR,0=>1st array,1=>2nd array,2=>3rd array ) its collapse. When it is array of 3, 2 and 1, then it works.

    Any idea? Is it memory problem? could it be?

  2. MariuszEm
    Member
    Posted 1 year ago #

    Did it.
    Just make it:

    array(
          'key' => 'filter1',
          'value' => 'value1' OR 'value2' OR 'value3',
          'compare' => 'LIKE'
        )

    Now I need to put values dynamicly.

  3. MariuszEm
    Member
    Posted 1 year ago #

    Of course it is not a solution. I was blind not seeing that.
    How to make it work?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this [untested]:

    $query_array = array('relation' => 'OR');
        array_push($query_array,
        array(
          'key' => 'filter1',
          'value' => array('value1', 'value2', 'value3'),
          'compare' => 'IN'
        )
      );

    https://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters

    Do you still get the error?

  5. MariuszEm
    Member
    Posted 1 year ago #

    No error, but no results also.

    $query_array = array('relation' => 'OR');
        array_push($query_array,
        array(
          'key' => 'filter1',
          'value' => array('value1', 'value2', 'value3'),
          'compare' => 'IN'
        )
      );

    No results.

    $query_array = array('relation' => 'OR');
        array_push($query_array,
        array(
          'key' => 'filter1',
          'value' => 'value3',
          'compare' => 'LIKE'
        )
      );

    Works like it should (but with one filter), but when I use array('value1', 'value2', 'value3') i got nothing again.

    PS. I am filtering by ACF value.

  6. MariuszEm
    Member
    Posted 1 year ago #

    One more think about link you posted - codex.
    There is line with code below "Display posts that have meta key 'color' NOT LIKE value 'blue' OR meta key 'price' with values BETWEEN 20 and 100:"

    I use exact code below that text, but I have 3 conditions, not 2. When you put 3rd condition sth goes wrong and SQL query makes it lost connection.

  7. MariuszEm
    Member
    Posted 1 year ago #

    And I got it. ACF makes meta value looks like:
    a:3:{i:0;s:10:"value1";i:1;s:11:"value2";i:2;s:15:"value3";
    So it is why I can't use "=" instead of LIKE. It is also why I can't use IN.

    IN works like "=", but it works in array. Is there LIKE for arrays?

Topic Closed

This topic has been closed to new replies.

About this Topic