1

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

1 Answer 1

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.

10
  • Hiiz Thanks for the help Kaiser :} im getting an empty list out, please see here. designzzz.com/test-short.php also added the code i used in the text area on this page. please see if im doing something wrong! thanks
    – Ayaz Malik
    Commented Apr 23, 2012 at 22:05
  • First: Set all the php error and WP DEBUG stuff to ON/true in your wp-config.php (search here on WPSE & in Codex & on php.net). Note: You have to have "` [fileurl=`" (leading space) - else it won't work.
    – kaiser
    Commented Apr 23, 2012 at 22:09
  • turned it on plz check again : designzzz.com/test-short.php .. my god can't believe a single plugin creating so many errors..
    – Ayaz Malik
    Commented Apr 23, 2012 at 22:17
  • That's not an error, just a Notice. There's a difference between Notice/Warning/Error. Btw: Whats {filelink? Shortcodes start with [` square brackets... Plus: Why's the php function inside a textarea???
    – kaiser
    Commented Apr 23, 2012 at 22:20
  • oh oh looks like i mistook it as a shortcode . did i ? its like this : {filelink=99} , {filelink=97} etc.... about the textarea i placed it to show you what code i am running. as in full
    – Ayaz Malik
    Commented Apr 23, 2012 at 22:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.