• Hey all
    I know this topic has been covered, and there is lots of info on it, but I cannot get it to work. Hoping for some help with my specific code.

    I am trying to limit the 3 posts showing here to only ones from the category “sample” (category id 39).

    I have tried adding cat=39 before showposts=3 but it did not work. Am I missing something. Can anyone help plz ??

    <?php $the_query = new WP_Query('showposts=3&post_type=listings') ?>
    	<?php if($the_query->found_posts == 0){
    		for($i=1; $i<=3; $i++){echo dummy_listings();}
    	}else{ ?>
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post();?>

    https://jessicataylorkeller.com/demo2/residential/

Viewing 9 replies - 1 through 9 (of 9 total)
  • What exactly is happening? You’re missing a closing bracket (and have an extra one in the wrong place) on your if/else statement.

    Try this:

    <?php 
    
    $the_query = new WP_Query('cat=39&showposts=3&post_type=listings')
    
    if($the_query->found_posts == 0){
        for($i=1; $i<=3; $i++){echo dummy_listings();
    }else{
        while ( $the_query->have_posts() ) : $the_query->the_post();
    }
    ?>

    Thread Starter Jessi

    (@wbjtk)

    that was only a snippet of the code, it works fine and came that way in the theme. so i will leave it as is…

    i’ve tried adding cat=39 before showposts=3 and it doesn’t do what i want it to

    any other ideas?

    Oh ok… if it’s a snippet.. leave it be.

    Ummm.. try the category name?

    $the_query = new WP_Query('category_name=sample&showposts=3&post_type=listings')
    Thread Starter Jessi

    (@wbjtk)

    thanks for your help josh. the “category” i want to use is a taxonomy (i have no idea what it all means, newbie here)- does that make a difference. would it be something other than a category?

    Lol – Yes.

    Okay, give this a quick read and it should help you:
    https://codex.www.ads-software.com/Class_Reference/WP_Query#Taxonomy_Parameters

    Let me know if you get stuck.

    Thread Starter Jessi

    (@wbjtk)

    oooooooh ok im learning as we go here lol

    a taxonomy is what a category class is???
    so i couldn’t understand that page for the life of me but my taxonomy is in functions.php here:

    register_taxonomy(
    		'sample',
    		'listings',
    		array(
    			'hierarchical' => true,
    			'label' => 'Sample Listings',
    			'query_var' => true,
    			'rewrite' => array('slug' => 'sample')
    		)
    	);

    would i be putting sample=sample before the showposts=3 ???
    so grateful for your help

    Thread Starter Jessi

    (@wbjtk)

    ok i answered my own question lol!

    $the_query = new WP_Query('sample=sample&showposts=3&post_type=listings')

    lesson learnt haha thanks josh

    Here… let’s try changing the above code (original paste) to this:

    <?php 
    
    $args = array(
    	'post_type' => 'listings',
            'showposts' => 3,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'sample'
    		)
    	)
    );
    
    $the_query = new WP_Query($args) ?>
    	<?php if($the_query->found_posts == 0){
    		for($i=1; $i<=3; $i++){echo dummy_listings();}
    	}else{ ?>
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post();?>

    Be sure to backup before making the change. This is untested.

    Lol – or do it that way ??

    Glad you got it working!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Show only posts from specific category’ is closed to new replies.