• Im using the following code to show some of the latest posts that are in all categories apart from category 3. $latestPost=get_posts('numberposts=3&cat=-3'); if i have cat=-3 in the query then it just shows posts from all categories, if i use category=-3 then no posts show, if i use &cat=4,5 then posts in all categories show and if i use category=4,5 it just shows the post that is in category 4

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    With get_posts you have to use “category”. Can you show us the whole foreach you are using to display the posts.

    Thread Starter oli_collins

    (@oli_collins)

    <? $latestPost=get_posts('numberposts=2&category=3');
    		foreach($latestPost as $pst){?>
            <? $contentAge=$pst->post_content; $contentAg=substr($contentAge,0,57); ?>
            <? $custom_fields = get_post_custom($pst->ID); $post_category = get_the_category($pst->ID); ?>
            <li><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/"><?=$pst->post_title?></a><br /><?=$contentAg?>...<br /><div class="more"><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/">More...</a></div></li>
            <? } ?>

    As per my initial post I have tried using category but there were still problems with it

    Moderator keesiemeijer

    (@keesiemeijer)

    Have you tried with setup_postdata. Like so:

    <? $latestPost=get_posts('numberposts=2&category=3');
    		foreach($latestPost as $pst){
    setup_postdata($pst);
    
    ?>
            <? $contentAge=$pst->post_content; $contentAg=substr($contentAge,0,57); ?>
            <? $custom_fields = get_post_custom($pst->ID); $post_category = get_the_category($pst->ID); ?>
            <li><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/"><?=$pst->post_title?></a><br /><?=$contentAg?>...<br /><div class="more"><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/">More...</a></div></li>
            <? } ?>

    Thread Starter oli_collins

    (@oli_collins)

    ok my code is now shown below and it shows no posts but its not relatest the setup_postdata($pst); line as it was like that before i just put that in:

    <? $latestPost=get_posts('numberposts=3&category=-3');
    		foreach($latestPost as $pst){
    			setup_postdata($pst);?>
             <? $custom_fields = get_post_custom($pst->ID); /*print_r($custom_fields);*/ $post_category = get_the_category($pst->ID); ?>
            <li><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/"><span><?=$pst->post_title?></span><br /><?php echo $custom_fields['Location'][0]; ?><? if($custom_fields['Salary'][0]!=''){?><br /><span><? echo $custom_fields['Salary'][0]; ?></span><? } ?></a></li>
            <? } ?>
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘get_posts category not working’ is closed to new replies.