excluding posts that contain specific string (3 posts)

  1. widecast
    Posted 3 years ago #

    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!


  2. vtxyzzy
    Posted 3 years ago #

    You can do this by adding filters to the query. See this article: http://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;
    global $mam_global_where;
    $mam_global_where = " AND $wpdb->posts.post_content NOT LIKE '%youtube.com%'";
    $mam_global_where = ''; // Turn off filter
  3. widecast
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.