• Resolved brokendisk

    (@brokendisk)


    I’ve created a custom taxonomy called ‘issue’. Posts can be assigned an issue such as ‘Magic’ or ‘Rainbow’ or whatever. On the index page I’m doing something like this:

    <?
    // get latest post with an issue id
    // set that issue id to $issue
    $issue = ‘magic’;
    $issue_query = new WP_Query(‘issue=’ . $issue . ‘&showposts=4’);

    while ($issue_query->have_posts()) : $issue_query->the_post();
    ?>

    What I’d like is explained in the commented lines. Get latest post that has an issue assigned to it and set $issue to it. The idea is that the issue assigned to posts will change maybe once every few months, and I’d like the displayed posts to update automatically when it does.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hello,

    I’m certain this can be improved upon, but here’s my solution after tinkering with it for a few minutes:

    add_shortcode('tax', 'tax_function');
    function tax_function() {
    	$issues = get_terms('issue');
    	$issue_array = array();
    
    	foreach($issues as $issue) {
    		$issue_array[] = $issue->slug;
    	}
    
    	$args = array( 'numberposts' => '1',
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'issue',
    				'field' => 'slug',
    				'terms' => $issue_array,
    			),
    		)
    	);
    
    	$recent_posts = wp_get_recent_posts( $args );
    	$term = wp_get_post_terms($recent_posts[0]["ID"], 'issue');
    	$the_issue = $term[0]->slug;
    
    	$loop_args = array( 'numberposts' => '10',
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'issue',
    				'field' => 'slug',
    				'terms' => $the_issue,
    			),
    		)
    	);
    
    	$the_query = new WP_Query( $loop_args );
    
    	if ( $the_query->have_posts() ) {
    		echo '<ul>';
    		while ( $the_query->have_posts() ) {
    			$the_query->the_post();
    			echo '<li>' . get_the_title() . '</li>';
    		}
    		echo '</ul>';
    	}
    }

    Obviously you’ll probably want to change it from using a shortcode to whatever implementation method you prefer, but this worked for me.

    I hope this is helpful.

    Cheers.

    Thread Starter brokendisk

    (@brokendisk)

    Hey Kendall,

    Thanks for taking the time to write that function out. It seems like nothing is being retrieved though and I’m getting NULL on a var_dump of $the_query.

    Any further advice or guidance?

    Many thanks!

    I really sort of meant for the above to be used as an example rather than a drop in fix. Make sure the args are correct for your site and echo the results of each variable along the way to figure out where the break is happening.

    Thread Starter brokendisk

    (@brokendisk)

    That’s just the thing I’m pretty new to WordPress development and don’t understand too well how it works. I’m dumping each variable and getting either nothing or NULL. The taxonomy term is “issue” so what you’ve written looks fine to me but I’m not really sure how to debug further.

    Any clues?

    Is the site on a server somewhere or do you have it on a localhost?

    Thread Starter brokendisk

    (@brokendisk)

    I’m working locally on a new theme but with a DB of an existing site.

    Can you whip up a backup of the site and database right quick, throw it up on a server or in Dropbox and email me a link?

    BackUpWordPress works well for this.

    kendall at peppermintmedia dot net

    Thread Starter brokendisk

    (@brokendisk)

    I figured it out, wasn’t calling the function correctly. What you had actually worked perfectly. Thanks Kendall!

    Glad I could help. If that’s it for this issue, would you mind marking the topic as “resolved” so that others in the future can know to reference it when they have similar issues?

    Thanks a bunch.

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

The topic ‘Get most recent post with custom taxonomy then filter by same’ is closed to new replies.