Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a simple custom shortcode plugin where you give it a tag and it'll display the titles of all the posts with that tag.

e.g. In my post if I type "[listposts tag=oct-2011]" it will output the titles of all posts tagged as "oct-2011".

This plugin has been working fine for me until the v3.2 upgrade.

Now, it still works fine when the post which uses the shortcode is viewed on its own. But when that post is viewed on the homepage, the get_posts() query returns 0 results.

What do I need to do to fix the plugin?

Here is the relevant code:

function listposts($atts) {
    $atts = shortcode_atts(
        array(
            'tag' = 'jan-2011',
            'numberposts' = '-1',
            'orderby' = 'post_date',
            'order' = 'asc',
            'post_status' = 'any' ),
        $atts);

    $catposts = get_posts($atts);
    $out = '';
    foreach ($catposts as $single) {
        $out .= $single->post_title;
    }

    return $out;
}
share|improve this question
    
Edit:- Please change 'numberposts' to 'posts_per_page'. –  Rutwick Gangurde Oct 4 '11 at 4:01

2 Answers 2

I checked your code locally, it works fine...

function listposts($atts) {
    $atts = shortcode_atts(array('tag' => 'lorem', 'posts_per_page' => -1, 'orderby' =>  'post_date', 'order' => 'asc', 'post_status' => 'any'), $atts);
    $catposts = get_posts($atts);
    $out = '';
    foreach ($catposts as $single) 
        {
            $out .= $single->post_title."<br />";
        }
    return $out;
    }

    add_shortcode('list', 'listposts');

Please recheck if you're entering the shortcode in the visual editor's HTML mode.

share|improve this answer
    
Hey Rutwick. Thanks for responding. I tried changing numberposts to posts_per_page but still no dice. I don't think I'm doing anything wrong with the shortcode because it works fine when I view the post as a single page. The get_posts function only returns a null set when the post with the shortcode is viewed within the homepage. I guess it might be a conflict with my specific setup (multisite)? –  markamgine Oct 11 '11 at 18:58

For get_posts argument suppose to be numberposts not posts_per_page and you were passing as string make it integer try it, this may help you

function listposts($atts) {

$atts = shortcode_atts( array( 'tag' => 'jan-2011', 'numberposts' => -1, 'orderby' =>  'post_date', 'order' => 'asc', 'post_status' => 'any' ), $atts );
$catposts = get_posts( $atts );
$out = '';
foreach ( $catposts as $single ) 
    {
        $out .= $single->post_title."<br />";
    }
return $out;
}

add_shortcode( 'list', 'listposts' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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