Magazine layout for wordpress : latest article for each category

a quick and efficient way of ensuring you have a permanent display of various articles on the front page of your wordpress blog is to alter the main query to get only one article per category. this can be done using specific join filters, as described on this wordpress support blog discussion. a very simple solution consists of including the alter-query.php file before the main query is executed. Here is the content of this file :

if (is_home())

{

 

function recent_join ( $join, $query ) {

global $wpdb;

$taxonomy = 'category';

$join .=<<<SQL

JOIN( SELECT MAX( {$wpdb->posts}.post_date ) AS post_date, trm.term_id AS term_ID FROM {$wpdb->posts}

LEFT JOIN {$wpdb->term_relationships} AS rel ON {$wpdb->posts}.ID=rel.object_ID

LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )

LEFT JOIN {$wpdb->terms} AS trm USING( term_id )

WHERE tax.taxonomy='{$taxonomy}' GROUP BY trm.term_id ) AS pmx USING( post_date )

SQL;

return $join;

}

add_filter ( 'posts_join', 'recent_join', 10, 2 );

 

function recent_fields ( $fields, $query ) {

global $wpdb;

return $fields . ', term_ID';

}

add_filter ( 'posts_fields', 'recent_fields', 10, 2 );

 

$args = array (

'post_type' => 'post',

'taxonomy' => 'category',

'paged' => get_query_var ( 'paged' ) ? get_query_var ( 'paged' ) : 1

);

)
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *