• Hi there,

    I’ve done some searching but have come up empty so far. I’m attempting to pull posts from the database that do not contain a specific string.

    For example, let’s say that want to pull posts from the database but with the condition that they do not have the string ‘youtube.com’ within the post content.

    Any ideas how I can achieve this?

    This is my current query, without any method of excluding a string.

    $posts = query_posts('showposts='.$numposts.'&orderby=rand');

    Any ideas are welcome!

    David

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can do this by adding filters to the query. See this article: https://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/

    Try coding your query something like this (UNTESTED):

    function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= ' ' . $mam_global_where;
       return $where;
    }
    add_filter('posts_where','mam_posts_where');
    
    global $mam_global_where;
    $mam_global_where = " AND $wpdb->posts.post_content NOT LIKE '%youtube.com%'";
    query_posts('showposts='.$numposts.'&orderby=rand');
    $mam_global_where = ''; // Turn off filter
    Thread Starter widecast

    (@widecast)

    vtxyzzy, I really appreciate that you shared this knowledge! It’s exactly what I was looking to do but didn’t know how to use filters to modify a query. Going to give this a shot. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘excluding posts that contain specific string’ is closed to new replies.