WordPress.org

Ready to get started?Download WordPress

Forums

excluding posts that contain specific string (3 posts)

  1. widecast
    Member
    Posted 1 year 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!

    David

  2. vtxyzzy
    Member
    Posted 1 year 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;
    }
    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
  3. widecast
    Member
    Posted 1 year 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

Tags

No tags yet.