Possible to display posts containing a short code ?

i have some posts with a certain shortcode in them. unfortunately these posts are in different categories and contain different tags so i am not able to query them. is it possible that i run a query which will display all posts containing this shortcode [fileurl=xx] where xx is a certain id (number).
Help is appreciated .
cheers

Related posts

Leave a Reply

1 comment

  1. Taxonomy Query

    Always use get_posts() for sidequeries. query_posts() is only for the Main query and new WP_Query() should be used for the second queries only. Read more here.

    You can query posts by a taxonomy (category, term, etc.):

    $cat_posts = new WP_Query( array(
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'movie_janner',
                'field' => 'slug',
                'terms' => array( 'action', 'commedy' )
            ),
            array(
                'taxonomy' => 'actor',
                'field' => 'id',
                'terms' => array( 103, 115, 206 ),
                'operator' => 'NOT IN'
            )
        )
    ) );
    

    Shortcode Query

    You could also, if you want to query by shortcode, add/remove a filter on the query for the content.

    Make shure that you read the comments carefully!

    function wpse49871_shortcode_query_filter( $where )
    {
        global $wpdb;
    
        // Lets be on the safe side, escape and such.
        $new_where = $wpdb->prepare( 
             "%s AND %s LIKE %s"
            ,$where
            ,"{$wpdb->posts}.post_content"
            // If you know the exact ID, then just insert it in here.
            ,like_escape( '%fileurl=%' )
        );
    
        return $new_where;
    }
    function wpse49871_get_shortcode_posts()
    {
        add_filter( 'posts_where', 'wpse_shortcode_query_filter' );
        $posts = get_posts( array(
            // Do your query in here. See "Taxonomy Query" args above for example
        ) );
    
        // Don't need it anymore after this run
        remove_filter( 'posts_where', 'wpse_shortcode_query_filter' );
    
        return $posts;
    }
    

    An even better approach may be to wrap it up in a class like the one shown in the other Q.