• Resolved nsnipes

    (@nsnipes)


    I’m trying to remove a specific category (id=3 from showing up in a section of my themes home page.

    I’ve been researching this for a few days and in theory I understand how this is done. But the theme is not coded the way the examples I’ve found are so I’m having trouble figuring out how to do this.

    Here is the section of code that I need to remove a category from:

    <h3><?php _e( 'Recent Post', 'app_post' ); ?></h3>
                    <?php
                    //  echo $wpc_no_post_index;
                        $recentQuery = "SELECT id, post_title,post_content,comment_count FROM {$wpdb->prefix}posts WHERE post_type='post' and post_status='publish' ORDER BY id DESC LIMIT $wpc_no_post_index";
                        $recent_posts = $wpdb->get_results($recentQuery, OBJECT);
                        foreach ($recent_posts as $recent) :
                            setup_postdata($recent);
                    ?>
                            <div id="post-<?php $recent->id; ?>">
                        <h2>
                    <?php
                            echo '<span class="app-index-category">';
                            the_category(', ', 'multiple', $recent->id);
                            echo '</span><span class="app-category-arrow"></span>';
                    ?>
                        <a class="app-index-title" href="<?php echo get_permalink($recent->id) ?> " >
                    <?php
                            echo substr($recent->post_title, 0, 60);
                    ?>
                        <!--</a>-->
                    </h2>
                    <?php
                            //Display the post image
                            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $recent->post_content, $matches);
                            $postImage = $matches[1][0];
    //                            if (empty($postImage)){//$postImage = get_bloginfo('template_directory') . '/images/no-post.png';
    //                            }
                            $postContent = substr(strip_tags($recent->post_content), 0, 400);
                            if (strlen($recent->post_content) > 400) {
                                $postContent .= '...';
                            }
                            if (empty($postImage)) {
                    ?>
                            <p class="app-recent-content">
                    <?php
                                if (strlen($recent->post_content > 400))
                                    echo " ...";
                                echo $postContent;
                        ?>
    
                            </p><?php } else {
                    ?>
                                <img alt="" src="<?php echo $postImage; ?>" width="298" height="" />
                    <?php } ?></a>
                            <div class="clear"></div>
                            <div class="readmore">
                                <!--<a href="<?php echo get_permalink($recent->id) ?>"><?php _e( 'Read', 'app_post' ); ?></a> |-->
                                <a href="<?php echo get_permalink($recent->id); ?>">
                                    <?php _e( 'Comments', 'app_post' ); ?>
                                </a>
                            </div>
                        </div><?php endforeach; ?>

    Can anyone help me figure this out?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try using this for your query:

    $recentQuery =
    "SELECT DISTINCT ID, post_title,post_content,comment_count
    FROM {$wpdb->prefix}posts
    WHERE post_type='post'
    AND post_status='publish'
    AND ID NOT IN (SELECT object_id FROM {$wpdb->prefix}term_relationships WHERE object_id = ID AND term_taxonomy_id = 3)
    ORDER BY ID DESC
    LIMIT $wpc_no_post_index
    ";
    Thread Starter nsnipes

    (@nsnipes)

    @vtxyzzy – Thanks for the reply.

    I have tried that exact that exact thing and it does remove the posts from that category. However, it also then does something very strange.

    All of the remaining posts (those that are not in category id=3) link to the same post (the first post of the removed category).

    Does that make any sense to you?

    The problem seems to be in the mixed capitalization of the posts column ‘ID’. The SELECT seems to ignore the capitalization, but the get_permalink() function does not.

    Try using ‘ID’ everywhere rather than ‘id’.

    Thread Starter nsnipes

    (@nsnipes)

    Perfect. Thanks you so much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Remove a specific category’ is closed to new replies.