Skip to content

Instantly share code, notes, and snippets.

@ahmad24
Last active December 30, 2015 03:09
Show Gist options
  • Save ahmad24/7767543 to your computer and use it in GitHub Desktop.
Save ahmad24/7767543 to your computer and use it in GitHub Desktop.
wordpress : custom filter
<?php
function boj_posts_by_comments() {
/* Default arguments. */
$args = array(
'post_type' = > 'post',
'posts_per_page' = > 10,
'order' = > 'DESC',
'orderby' = > 'comment_count'
);
/* Apply filters to the arguments. */
$args = apply_filters( 'boj_posts_by_comments_args', $args );
/* Set up the output variable. */
$out = '';
/* Query posts from the database by the given arguments. */
$loop = new WP_Query( $args );
/* Check if posts are found. */
if ( $loop-> have_posts() ) {
/* Open the unordered list. */
$out .= ' < ul class="posts-by-comments" > ';
/* Loop through the posts. */
while ( $loop-> have_posts() ) {
$loop-> the_post();
/* Add the post title to the list. */
$out .= the_title( ' < li > ', ' < /li > ', false );
}
/* Close the unordered list. */
$out .= ' < /ul > ';
}
/* Apply filters to the final output. */
$out = apply_filters( 'boj_posts_by_comments', $out );
/* Display the HTML. */
echo $out;
}
//*************************
add_filter( 'boj_posts_by_comments_args', 'boj_change_posts_by_comments_args' );
function boj_change_posts_by_comments_args( $args ) {
/* Change the value of the posts_per_page array key. */
$args['posts_per_page'] = 15;
/* Return the $args parameter. */
return $args;
}
//**************************
add_filter( 'boj_posts_by_comments', 'boj_change_posts_by_comments' );
function boj_change_posts_by_comments( $out ) {
/* Change the opening < ul > to an < ol > . */
$out = str_replace( ' < ul ', ' < ol ', $out );
/* Change the closing < /ul > to an < /ol > . */
$out = str_replace( ' < /ul > ', ' < /ol > ', $out );
/* Return the filtered HTML. */
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment