WordPress.org

Ready to get started?Download WordPress

Forums

Multiple $Get_Posts? (6 posts)

  1. ianbee
    Member
    Posted 1 year ago #

    I have this plugin in WordPress that I've heavily modified. The purpose of the plugin is originally to display thumbnails for whatever category you tell it to. As of right now, I've made it done much more than that. But anyways, here is the shortcode for the plugin..

    [categorythumbnaillist 7]
    7 being the category ID of course. The plugin gets the posts for whatever category and orders them using this code that I made:

    $args=array(
              'showposts'=>5,
    	  'category'=> $listCatId[1],
    	  'orderby'=> $categoryThumbnailList_OrderType,
    	  'order'=> $categoryThumbnailList_Order
    
            );
    	$myposts = get_posts( $args);

    Now, I wanted the plugin to only show posts if it has has the tag "news" in it. So I did the news tag in like so:

    $args=array(
              'tag' => 'news',
              'showposts'=>5,
    	  'category'=> $listCatId[1],
    	  'orderby'=> $categoryThumbnailList_OrderType,
    	  'order'=> $categoryThumbnailList_Order
    
            );
    	$myposts = get_posts( $args);

    $myposts = get_posts( $args );
    This sucesfully displays 5 posts if it has the "news" tag in it. But here is the problem...

    I'm going to be using this plugin multiple times on one page. So when I use the shortcode listed above with a different category ID, the plugin doesn't display posts for the respective category since there are no posts tagged with "news" other than in the news category. :(

    I'm going to be using the plugin with my news category, photos category and audio category. I would like it to display each categories thumbnails via shortcode (like the plugin is intended to do) but also only display the news posts with the tags "news"... for only the news category. How would I make it function properly and display only news posts with the tag "news" while still displaying the other category posts properly... all via shortcode like the plugin is supposed to do? So for example... I'd have..

    [categorythumbnaillist 3] (news category)
    [categorythumbnaillist 5] (photos category)
    [categorythumbnaillist 7] (audio category)

    I'd like news to only display news posts with the tag "news", photos to display the photos category posts and audio to display the audio category posts. Again, I've already figured out how to do one or the other, I just don't know how to make the code do both.

    Any help would truely be appreciated!!! :)

  2. ianbee
    Member
    Posted 1 year ago #

    I tried making two different functions for get_posts and then running an if / else statement like so...

    $args=array(
              'tag' => 'news',
              'showposts'=>5,
    	  'category'=> $listCatId[1],
    	  'orderby'=> $categoryThumbnailList_OrderType,
    	  'order'=> $categoryThumbnailList_Order
            );
    
    	$targs=array(
              'showposts'=>5,
    	  'category'=> $listCatId[1],
    	  'orderby'=> $categoryThumbnailList_OrderType,
    	  'order'=> $categoryThumbnailList_Order
    
            );
    
    if ( tag == true ) {
    	$myposts = get_posts( $args);
    }
    
    else {
    	$myposts = get_posts( $targs);
    }

    This sucesfully filtered the news posts to only show posts tagged with "news" in them.. but then it doesn't get posts for any other categories. :(

    Again... any help would be appreciated sooo much! :D

  3. bcworkz
    Member
    Posted 1 year ago #

    You're on the right track with your if/else logic, but your conditional check of tag == true is bad syntax. What is 'tag', do you mean '$tag'? Where/how is this set?. You should be getting the category id passed to your shortcode handler and using that for a conditional test.

  4. ianbee
    Member
    Posted 1 year ago #

    Hey man, thanks for you help I really appreciate it... the following codes have actually filtered out the posts with the tag "news" successfully, but doesn't show the other posts in different categories.

    if ( $tag = news ) {
    	$myposts = get_posts( $args);
    }
    
    else {
    	$myposts = get_posts( $targs);
    }

    and this one works aswell suprisignly:

    if ( tag == true) {
    	$myposts = get_posts( $args);
    }
    
    else {
    	$myposts = get_posts( $targs);
    }

    If I do something like:

    if ( $tag ) {
    	$myposts = get_posts( $args);
    }
    
    else {
    	$myposts = get_posts( $targs);
    }

    It shows all the posts for every category but doesn't filter out the news posts. I think its because I'm not defining a tag so it uses get_posts ($targs) where targs doesn't filter out any posts.

    I've tried using this code, but I'll tell you why it isn't working..

    if ( is_category('news') ) {
    	$myposts = get_posts( $args);
    }
    
    else {
    	$myposts = get_posts( $targs);
    }

    I've also tried "in_category".. but both don't filter out the news at all because the posts aren't really in the category news. I mean they are technically published under the news category but they're not being displayed thru the news category, there being displayed through the plugin. I think that's why the is_category doesn't work. lol!

    But, I need to find a way to tell the plugin if posts are in the news category... then filter and only display posts with the tag "news"... and if there not it the news category.. just display them regardless of tags.

    Thanks for the help so far, I think I'm close to figuring this out! Any more help would be awesome! :)

  5. ianbee
    Member
    Posted 1 year ago #

    I tried changing the $args to only show the news category but now for some reason all the categories run $args, instead of the news just running $args and everything else running $targs... This is my code:

    $args=array(
              'tag' => 'news',
              'showposts'=> 5,
    	  'category'=> '3', <- "3" is my news category :)
    	  'orderby'=> $categoryThumbnailList_OrderType,
    	  'order'=> $categoryThumbnailList_Order
            );
    
    	$targs=array(
              'showposts'=> 5,
    	  'category'=> $listCatId[1],
    	  'orderby'=> $categoryThumbnailList_OrderType,
    	  'order'=> $categoryThumbnailList_Order
    
            );
    
    if ( $tag = news ) {
    	$myposts = get_posts( $args);
    }
    
    else {
    	$myposts = get_posts( $targs);
    }
  6. Chris
    Member
    Posted 1 year ago #

    Maybe it's just a typo but instead of

    if ( $tag = news ) {
    	$myposts = get_posts( $args);
    }

    try

    if ( $tag == 'news' ) {
    	$myposts = get_posts( $args);
    }

Topic Closed

This topic has been closed to new replies.

About this Topic