0

I am using this code to get stuff based on my custom field and custom content type.

$searchTerm = 'bestafricanvideos';

$args = array(
    'post_type' => 'sitemaps',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key'     => 'cstm_sitemap_url',
            'value'   => $searchTerm,
            'compare' => 'LIKE',
        ),
    )
);

print_r($args);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :

    while ( $the_query->have_posts() ) : $the_query->the_post();

        $video_url = rwmb_meta('cstm_sitemap_url');
        print_r($video_url);

    endwhile;

else : 

    echo "No video URL found";

    exit;

endif;

wp_reset_postdata();

print_r($video_url);

And the problem is that nothing is returned.

If I remove the metaquery I get a lot of results.

So, I think the problem is that I have stored the custom meta field in db as an url, for example: http://bestafricanvideos.com/top10.html and my search term is bestafricanvideos so, maybe the LIKE cannot grab it and only something like this: http:// bestafricanvideos .com/top10.html will be grabbed?

Any idea how to fix the meta_query so I can grab stuff that conatain the search term in a longer string (or part of a string)?

3
  • That LIKE query will be a wildcard query-- %searchterm%-- so it should match any of the strings you provided as samples. Exactly where in the code does is "return nothing"? I assume you've debugged by, for example, checking var_dump($the_query->request);?
    – s_ha_dum
    Commented Nov 7, 2015 at 14:40
  • Hmm it seems to be something with wordpress escaping the string. E.g. If i put the whole url int othe query the result is this: '%http://bestafricanvideos.com/videos\\_top\\_10.xml%' ) Commented Nov 7, 2015 at 14:55
  • Interesting. I don't see that effect
    – s_ha_dum
    Commented Nov 7, 2015 at 15:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.