• I’m using this code to pull the most commented posts from the entire database. I was wondering how I can edit it so that it only pulls posts from a select few categories and not the entire database.

    <ul>
    					<?php
    						$sql = "
    							SELECT *
    							FROM $wpdb->posts
    							WHERE 	post_date_gmt > ".(time()-strtotime('-2 weeks'))." AND post_type = 'post'
    							ORDER BY comment_count DESC LIMIT 5
    						";
    
    						$top_posts = $wpdb->get_results($sql);
    
    						foreach ($top_posts as $post)  {
    							?>
    							<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span><a class="comments" href="<?php the_permalink(); ?>#comments"><?php echo $post->comment_count; ?> Comments</a>Posted Under: <?php the_category(', '); ?></span></li>
    							<?
    						}
    					?>
    					</ul>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter JGrizzLee

    (@jgrizzlee)

    anybody there? =/

    Thread Starter JGrizzLee

    (@jgrizzlee)

    bump =(

    Why are you using an sql query, let the WordPress API do the work, look at WP_Query

    Category Parameters

    Show posts associated with certain categories.
    ■ cat (int) – use category id.
    ■ category_name (string) – use category slug (NOT name).
    ■ category__and (array) – use category id.
    ■ category__in (array) – use category id.
    ■ category__not_in (array) – use category id.

    Untested Code:

    <?php
    	$wp_query= null;
    	$wp_query = new WP_Query();
    	$args = array(
    		'post_type' => 'post',
    		'cat' => 10,
    		'orderby' => 'comment_count',
    		'showposts' => '10',
    		'&paged' => $paged,
    	);
    	$wp_query->query( $args );
    ?>
    
    <?php if ( $wp_query->have_posts() ) : ?>
    
    	<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    			<span>
    			<a class="comments" href="<?php the_permalink(); ?>#comments"><?php echo $post->comment_count; ?> Comments</a>Posted Under: <?php the_category(', '); ?>
    			</span>
    		</li>
    
    	<?php endwhile; ?>
    <?php endif; ?>

    HTH

    David

    Thread Starter JGrizzLee

    (@jgrizzlee)

    I didn’t create the theme, so I can’t answer that question. I would assume the author did it this way so that it can pull posts from a specific date range. I appreciate your help, but the code you posted doesn’t seem to have anything that specifies a certain date range to pull new posts. Also, to answer your first question, it could be because the API doesn’t work when used in the theme. You can see an example of it working (without categories specified like I need) here and the edited version with your code that just shows a blank here, when you look in the top right corner under “Top Articles”.

    I thought you were the developer that was writing the code, that was is why I asked.

    Nevermind there are always different ways, I only use sql queries as a last resort, the built in WordPress API calls are just wrapped functions, and do all the joins for us.

    Let me explain the structure, the post_date and comment_count are columns (fields) in the wp-post table, where as the post categories (many to one) are in the wp-term-relationship, so we would have to do a join on the two tables, however this is done for us by WP_Query.

    It is a handy bit of code, I will use it for a sidebar widget, so I have changed it to filter by category_name, this is the category slug lowercase and no spaces, and set this to featured so you could test.

    I have also added on a date filter function, this is from the WP_Query link above.

    http://pastebin.com/WDAg54bu

    It should just need some posts dated in the last 14 days, and in the featured category.

    Please test and let us know how you get on!

    HTH

    David

    Thread Starter JGrizzLee

    (@jgrizzlee)

    Yep, that works! Big thanks for taking the time to help when nobody else would =D

    Thread Starter JGrizzLee

    (@jgrizzlee)

    One thing I forgot to ask, if I want to specify multiple categories would this be the proper way to do so?

    'category_name' => 'featured,catslug2,catslug3',

    You would need the category IDs, category_name will not take an array and is only for a singular value.

    Admin > Posts > Categories
    Find the ones you want and click on edit, in the url you will see the tag_ID, something like this.
    edit-tags.php?action=edit&taxonomy=category&tag_ID=106&post_type=post ,make a note of the ID’s this example is 106.

    category__in (array) – use category id.

    I think that this will work,change the category_name to
    category__in => array('106','210','255'),

    Not sure to use if cat => array(), or category_in => array(), as a post might be in two of the categories, make sure it will not show twice, you will need to test this and be aware.

    Use category_and if the post has to belong to all selected categories, like ‘news’ and ‘featured’

    Multiple Category Handling

    Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:
    $query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

    To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):
    $query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );

    You can also exclude multiple categories this way:
    $query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );

    If this works mark this as resolved please.

    HTH

    David

    I just had another look at the Codex, athough the category_name does not show an array option the code does.

    $query = new WP_Query( 'category_name=staff,news' );

    In your test sites code try:
    'category_name' => 'featured,in-theaters',
    or
    'category_name' => array('featured','in-theaters'),

    David

    Thread Starter JGrizzLee

    (@jgrizzlee)

    'category_name' => 'featured,in-theaters',

    seems to work on the test site without double posting, hopefully it will all work in a couple days when I bring everything over to the main site. thanks again.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘(Please Move to Advanced) Modifying sql to pull posts from select categories’ is closed to new replies.