WordPress.org

Support

Support » How-To and Troubleshooting » get_posts with meta_compare='LIKE' not working

get_posts with meta_compare='LIKE' not working

  • Hi,

    I am using the following code:

    $tolettpe = "Sale";//default
    if($_REQUEST['tolettype']) $tolettpe = $_REQUEST['tolettype'];
    else if($_REQUEST['srch_type']) $tolettpe = $_REQUEST['srch_type'];
    $args = array(
        'numberposts'  => $latestcount,
        'category'     => $catidstr,
      'meta_key'     => 'property_type',
      'meta_compare' => 'LIKE',
      'meta_value'   => $tolettpe.'%'
     );
    $post_content = get_posts($args);

    The value in the database is ‘Sale||’ and there are no query string variables in the request.

    But the query returns no results.

    If I use the exact value and no meta_compare, it works.

    Any ideas how to make this work?

Viewing 1 replies (of 1 total)
  • Hi, ranioli.

    Have you try nesting meta_query?

    $tolettpe = "Sale";//default
    if($_REQUEST['tolettype']) $tolettpe = $_REQUEST['tolettype'];
    else if($_REQUEST['srch_type']) $tolettpe = $_REQUEST['srch_type'];
    $args = array(
        'numberposts'  => $latestcount,
        'category'     => $catidstr,
        'meta_query'   => array(
        array(
            'key'           => 'property_type',
            'compare'       => 'LIKE',
            'value'         => $tolettpe.'%'
            )
        )
     );
    $post_content = get_posts($args);

    (Source)

    When I find some troubles with retrieving posts with custom fields, this should be the best form. Anyway, that’s my experience.

    Hope that helps!

Viewing 1 replies (of 1 total)
  • The topic ‘get_posts with meta_compare='LIKE' not working’ is closed to new replies.