Support » Plugins » Hacks » $exclude code and $sql code are not filtering correctly

  • So I have a blog,, any everything was working fine but recently we had to add an $exclude code because admins were showing up, even though they did not have posts, here is the code we added:

    			$excluded = '824, 265, 1691'; // To exclude Admin accounts
    			$sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE post_author NOT IN ($excluded) and post_status != 'trash' ORDER BY ID DESC LIMIT 7";
    			$authors = $wpdb->get_results($sql);
    			foreach($authors as $author):

    The issue is that the “Who Bloggin'” list used to update with the most recent blogger who actually submitted a post. This functionality does work if I remove the “post_author NOT IN ($excluded) and” part from the above code but if I remove that it then shows the 3 admins even though they do not have posts. Does anyone know a work around to get this Blog list to update but also keep the $exclude code in there or even a work around for that as well.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Two weeks, No Response, this forum is very bad

    I’m not familiar with using “NOT IN” in MySQL, but the searching I’ve done thus far suggest that it is intended more for cross table searches, rather than a query like you are doing.

    What happens if you try:

    $sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE post_author != 824 and post_author != 265 and post_author != 1691 and post_status != 'trash' ORDER BY ID DESC LIMIT 7";

    P.S. most folks in this forum volunteer their time and don’t necessarily have the time to do Google searching for you. If someone doesn’t respond, repost your query politely and add anything you may have done yourself to solve the issue.

    Well what specifically is the problem with the query, the query works exactly as it should do as far as i can test, what specifically is wrong with it? Help me help you… 🙂

    Well, the problem that was occurring was the Admin accounts were showing up in the list of the bloggers. The blogger list is suppose to change to the most recent blogger everytime they post a new post. The problem was that when I used that $exclude code, the bloggers stopped updating when a new blogger would post. As of this moment though since the bloggers are becoming more active, the Admin accounts have been pushed off, so even with the $exclude code gone our Admin accounts are not showing, so for the moment everything is fine, but thanks for all who replied. If the problem arises again I will try that $sql code above, that may be a solution for sure.

    Wow, so funny right after I wrote this post, I went to the blog just to check it out to make sure it was working, and low and behold, one of the Admin accounts showed up again. This was because I removed the “NOT IN ($excluded)” from the statement. I went ahead and I tried Tim’s code above but basically it gave me the same result as the $exclude code.

    What is happening is that the blogger list is suppose to re-arrange itself when a new post gets posts, by putting the blogger who posted the newest post, it the number one spot on the homepage. Here is my blog:
    You can see that the first blogger does not match the most recent post but as soon as I remove the “NOT IN ($excluded)” from the statement or the “post_author != 824 and post_author != 265 and post_author != 1691 and” the list appears correctly but adds the Admin accounts back in.

    Does anyone know another way to write this sql statement so that my Admin accounts do not show up but also that shows the correct order of the bloggers who blog frequently.

    Unless you can provide a way for me to test for the problem i don’t know what to suggest, the query works just fine for me(obviously with differing IDs), but without being able to reproduce the issue i can’t see how else to advise you.

    OK so I did some research and got it working, there maybe a better way to write the code but here is what I got:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    				<h2><?php the_title(); ?></h2>
    				<?php the_content(); ?>
    			<?php endwhile; endif; ?>
    $args = array( 'numberposts' => 15, 'order'=> 'DESC', 'orderby' => 'post_date' );
    $postslist = get_posts( $args );
    $authorlist = array("0");
    $rowcounter = 0;
    foreach ($postslist as $post) :  setup_postdata($post);$getid =$post->post_author;
        <?php if(!in_array($post->post_author,$authorlist,false) && $rowcounter <6) { ?>
        <div class="author" id="author-<?php the_author_meta('user_login', $post->post_author); ?>">
        <a href="<?php bloginfo('url'); ?>/author/<?php the_author_meta('user_nicename', $post->post_author); ?>"> <span class="read-blog">Read</span>
          <?php if(get_the_author_meta('description', $post->post_author)): ?>
          <!-- Author Avatar -->
          <div class="description"> <?php echo get_avatar(get_the_author_meta('user_email', $post->post_author), 100); ?> </div>
          <?php endif; ?>
          <ul class="info">
            <li class="name">
              <?php the_author_meta('display_name', $post->post_author); ?>
            <?php if(get_the_author_meta('location', $post->post_author) || get_the_author_meta('blog', $post->post_author)): ?>
            <?php if(get_the_author_meta('location', $post->post_author)): ?>
            <li class="location">
              <?php the_author_meta('location', $post->post_author); ?>
            <?php endif; ?>
          <?php endif; ?>
          </a><?php $authorlist[] = $post->post_author;$rowcounter=$rowcounter+1;?> </div>
         <?php }?>
        <?php endforeach; ?>

    Hope this helps others

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘$exclude code and $sql code are not filtering correctly’ is closed to new replies.