Skip to content

Instantly share code, notes, and snippets.

@doiftrue
Last active January 10, 2024 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doiftrue/c9ff95c91cef060612f0908f6c6febc6 to your computer and use it in GitHub Desktop.
Save doiftrue/c9ff95c91cef060612f0908f6c6febc6 to your computer and use it in GitHub Desktop.
[wp-kama embed] Previous Entries from Category >>> https://wp-kama.com/2102
<?php
/**
* Previous posts from the category (relative to the current post) + circular linking
* Parameters passed to the function. Default values are indicated in parentheses.
*
* @param int $post_num (5) Number of links.
* @param string $format ('') Output format: `{thumb} {date:j.M.Y} - {a}{title}{/a} ({comments})`.
* @param string $cache ('') Enable cache (default is off). Write 1 to enable.
* @param string $list_tag (li) List tag.
* @param string $post_type (post) Type of post we are working with.
* @param bool $echo (true) Output to screen or return for processing (false).
*
* @version 1.1
*/
function kama_previous_posts_from_cat( $args ){
global $post, $wpdb;
$args = (object) wp_parse_args( $args, [
'post_num' => 5,
'format' => '',
'cache' => true,
'list_tag' => 'li',
'post_type' => 'post',
'echo' => true,
] );
$cache_key = md5( __FUNCTION__ . $post->ID );
$cache_flag = __FUNCTION__;
if( $args->cache && $cache_out = wp_cache_get( $cache_key, $cache_flag ) ){
if( $args->echo ){
return print( $cache_out );
}
return $cache_out;
}
$cat = get_the_category( $post->ID );
$cat_id = (int) $cat[0]->term_id;
$sql_SELECT = "SELECT ID, post_title, post_date, comment_count, guid
FROM $wpdb->posts p
LEFT JOIN $wpdb->term_relationships rel ON (p.ID = rel.object_id)
LEFT JOIN $wpdb->term_taxonomy tax ON (rel.term_taxonomy_id = tax.term_taxonomy_id)";
$same_AND = $wpdb->prepare(
"AND tax.term_id = %s AND tax.taxonomy = 'category' AND p.post_status = 'publish' AND p.post_type = %s",
$cat_id, $args->post_type
);
// try to get previous posts
$sql = "$sql_SELECT WHERE p.ID < $post->ID $same_AND ORDER BY p.post_date DESC LIMIT " . (int) $args->post_num;
$res = $wpdb->get_results( $sql );
$count_res = count( $res );
// if the count is less than required, make a 2nd request
if( ! $res || $count_res < $args->post_num ){
$exclude = array_merge( [ $post->ID ], wp_list_pluck( $res, 'ID' ) );
$exclude = implode( ',', array_map( 'intval', $exclude ) );
$post_num = (int) $args->post_num - $count_res;
$sql = "$sql_SELECT WHERE p.ID NOT IN ($exclude) AND p.ID != {$post->ID} $same_AND
ORDER BY p.post_date DESC LIMIT " . (int) $post_num;
$res2 = $wpdb->get_results( $sql );
$res = array_merge( $res, $res2 );
}
if( ! $res ){
return false;
}
// output
if( $args->format ){
preg_match( '!{date:(.*?)}!', $args->format, $date_m );
}
$add_thumb = ( false !== strpos( $args->format, '{thumb}' ) );
$out = $x = '';
foreach( $res as $pst ){
$x = ( $x === 'li1' ) ? 'li2' : 'li1';
$a1 = '<a href="' . get_permalink( $pst->ID ) . '" title="' . esc_attr( $pst->post_title ) . '">';
$a2 = "</a>";
if( $args->format ){
$date = apply_filters( 'the_time', mysql2date( $date_m[1], $pst->post_date ) );
$com_count = $pst->comment_count ?: '';
$formatted = strtr( $args->format, [
$date_m[0] => $date,
'{title}' => esc_html( $pst->post_title ),
'{a}' => $a1,
'{/a}' => $a2,
'{comments}' => $com_count,
] );
// thumbnail exists
if( isset( $add_thumb ) ){
$thumb = get_the_post_thumbnail( $pst->ID, 'thumbnail' );
$formatted = str_replace( '{thumb}', $thumb, $formatted );
}
}
else{
$formatted = $a1 . esc_html( $pst->post_title ) . $a2;
}
$out .= apply_filters( 'kama_previous_posts_from_cat__append_out',
"\n<$args->list_tag class=\"$x\">$formatted</$args->list_tag>",
$args, $formatted, $x, $pst
);
}
if( $args->cache ){
wp_cache_add( $cache_key, $out, $cache_flag );
}
if( $args->echo ){
return print $out;
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment