Customizable Post Listings – not able to limit category listing
-
I’ve installed Customizable Post Listings 1.5 in order to get different listings of certain categories listed in a sidebar or within a Page.
I know that the code for this plugin is old and I fixed the problem with “Pages” showing up as new “Posts” a la this suggestion, which did the trick for that issue. However, there’s another problem with it in that the ability to pick a category or categories to list is not working.
So, I am using the syntax suggested by the plugin’s author, like this:
<?php c2c_get_recent_posts(3, “
<li>
%post_URL%</li>
“, “4”); ?>but I am still getting all recent posts from all categories (I created a new test category just to be sure) instead of being able to narrow it down to some or even just one category (category 4, the “Upcoming Events” category I want to list).
The place you can see the end result is here. You have to View Code and search for “event” and you’ll find it because I used HTML comment tags (I don’t want to go live with the results until it works!). Here’s the end result:
<li><a href="https://www.kristinforlowell.com/2007/07/16/test-numero-one/" title="View post Test Numero One">Test Numero One</a></li> <li><a href="https://www.kristinforlowell.com/2007/07/16/cuts-to-family-shelters-jam-plan/" title="View post Cuts to Family Shelters, JAM Plan">Cuts to Family Shelters, JAM Plan</a></li> <li><a href="https://www.kristinforlowell.com/2007/07/13/garden-party-fundraiser/" title="View post Midsummer Garden Gala, July 18th">Midsummer Garden Gala, July 18th</a></li>
The only part that should be showing up is the last one, the Midsummer Garden Gala. Everything else is not in category 4, but in other categories!
I’ve looked at the plugin code (I even changed the coded default value for the c2c_get_recent_posts() $categories variable, to no avail). Help!!
-
FYI, I did find something – the code wasn’t using wp_posts and wp_comments and wp_categories for the sql table calls. I changed that but it still isn’t working.
Here’s the SQL generated by the code:
SELECT DISTINCT * FROM wp_posts LEFT JOIN wp_post2cat ON (wp_posts.ID = wp_post2cat.post_id) AND ( category_id = 4) WHERE wp_posts.post_date <= '2007-07-16 13:15:53' AND ( wp_posts.post_status = 'publish' ) AND ( wp_posts.post_type = 'post' )
I’m still newish to the table/db structure, can anyone else find something wrong with that SQL right off the bat that would cause an issue?
*bump* please, can someone help me? I’m really at my wits’ end…tried everything I could think of to make the SQL only pick out the category in question, but it’s getting all the categories.
I’m going to go ahead and bump this because I’m having the same problem and it’s driving me absolutely nuts. The plugin author seems to have abandoned it and no one I have contacted about this seems to be able to help me out.
If there is no solution to this, does anyone know of a similar plugin? I really don’t want to let it go because it’s fantastic for a sketchblog, but it’s just not doing what I need it to do. ??
I agree…at this point, I’d even throw in a little pay (if I can get my clients to finally pay <i>me</i>) because literally, this is the last element I need to upgrade my Left in Lowell blog to 2.1x, with the new user features I’ve been promising my readers. (Heck, I’ll even do some fundraising on my blog to get the cash to get this done, they’re as eager as I am to upgrade.)
Anyone?
If this does get fixed and/or someone wants to talk about getting this done, my email is lynne at leftinlowell.com. Thanks…
missnina: At least I know I’m not insane and someone else had the same issue!
Arg, reset the thread to “not resolved” – hopefully that helps it show up where it can be looked at. Sorry about that – still sorta a newbie (but I did stay at a Holiday Inn Express last night – and read the sticky thread about “resolved” and “not resolved” statuses). ??
I combined several posts/fixes from the forum and the following works for me!
Limitation to category works
Page/Post fix is included.<?php
/*
Plugin Name: Customizable Post Listings
Version: 1.1
Plugin URI: https://www.coffee2code.com/wp-plugins/
Author: Scott Reilly
Author URI: https://www.coffee2code.com
Description: Display Recent Posts, Recently Commented Posts, Recently Modified Posts, Random Posts, and other post listings using the post information of your choosing in an easily customizable manner. You can narrow post searches by specifying categories and/or authors, among other things. Changes made by Adam.*/
/*
Copyright (c) 2004 by Scott Reilly (aka coffee2code)Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*///
// ************************ START TEMPLATE TAGS ******************************************************************
//function c2c_get_recent_posts ($num_posts = 5,
$format = ”- %post_date%: %post_URL%
- %post_date%: %post_URL%
- %comments_URL%
%last_comment_date%
%comments_fancy% - %post_URL%
Updated: %post_modified%
“,
$categories = ”, // space separated list of category IDs — leave empty to get all
$orderby = ‘date’,
$order = ‘DESC’, // either ‘ASC’ (ascending) or ‘DESC’ (descending)
$offset = 0, // number of posts to skip
$date_format = ‘m/d/Y’, // Date format, php-style, if different from blog’s date-format setting
$authors = ”, // space separated list of author IDs — leave empty to get all
$include_passworded_posts = false)
{
global $wpdb, $tablecomments, $tableposts, $tableterm_relationships, $tableterm_taxonomy;
if ($add_recent_comment_to_sql && !isset($tablecomments)) $tablecomments = $wpdb->comments;
if (!isset($tableterm_relationships)) $tableterm_relationships = $wpdb->term_relationships;
if (!isset($tableterm_taxonomy)) $tableterm_taxonomy = $wpdb->term_taxonomy;
if (!isset($tableposts)) $tableposts = $wpdb->posts;
if ($order != ‘ASC’) $order = ‘DESC’;
if (‘max_comment_date’ == $orderby) { $add_recent_comment_to_sql = 1; }
else {
if ($orderby != ‘rand()’) $orderby = “$tableposts.post_$orderby”;
$add_recent_comment_to_sql = 0;
}
$include_sticky_posts = true;$now = current_time(‘mysql’);
if ($add_recent_comment_to_sql) $sql = “SELECT $tableposts.*, MAX(comment_date) AS max_comment_date FROM $tablecomments, $tableposts “;
else $sql = “SELECT DISTINCT * FROM $tableposts “;
if ($categories) {
$sql .= “LEFT JOIN $tableterm_taxonomy ON ($tableterm_relationships.term_taxonomy_id = $tableterm_taxonomy.term_taxonomy_id) LEFT JOIN $tableterm_relationships ON ($tableposts.ID = $tableterm_relationships.object_id)”;
$cats = explode(‘ ‘, $categories);
}
$sql .= “WHERE $tableposts.post_date <= ‘$now’ AND ( $tableposts.post_status = ‘publish’ AND $tableposts.post_type = ‘post’ “;
if ($include_sticky_posts) $sql .= “OR $tableposts.post_status = ‘sticky’ “;
$sql .= “) “;
if (!$include_passworded_posts) $sql .= “AND $tableposts.post_password = ” “;
if ($add_recent_comment_to_sql) $sql .= “AND $tableposts.ID = $tablecomments.comment_post_ID AND $tablecomments.comment_approved = ‘1’ “;
if ($categories) {
$first = 1;
$sql .= “AND ( “;
foreach ($cats as $cat) {
if ($first) $first = 0;
else $sql .= “OR “;
$sql .= “$tableterm_relationships.term_taxonomy_id = ‘$cat’ “;
}
$sql .= “) “;
}
if ($authors) {
$auths = explode(‘ ‘, $authors);
$first = 1;
$sql .= “AND ( “;
foreach ($auths as $author) {
if ($first) $first = 0;
else $sql .= “OR “;
$sql .= “$tableposts.post_author = ‘$author’ “;
}
$sql .= “) “;
}
if (‘modified’ == $orderby) $sql .= “AND $tableposts.post_modified_gmt <= ‘$now’ “;
$sql .= “GROUP BY $tableposts.ID ORDER BY $orderby $order”;
if ($num_posts) $sql .= ” LIMIT $offset, $num_posts”;
$posts = array();
$posts = $wpdb->get_results($sql);
if (empty($posts)) return;
return c2c_get_recent_handler($posts, $format, $date_format);
} //end function c2c_get_recent_posts()function c2c_get_random_posts($num_posts = 5,
$format = ”“,
$categories = ”, // space separated list of category IDs — leave empty to get all
$order = ‘DESC’, // either ‘ASC’ (ascending) or ‘DESC’ (descending)
$offset = 0, // number of posts to skip
$date_format = ‘m/d/Y’, // Date format, php-style, if different from blog’s date-format setting
$authors = ”, // space separated list of author IDs — leave empty to get all
$include_passworded_posts = false)
{
return c2c_get_recent_posts($num_posts, $format, $categories, ‘rand()’, $order, $offset, $date_format, $authors, $include_passworded_posts);
} //end function get_random_post()function c2c_get_recently_commented ($num_posts = 5,
$format = ”“,
$categories = ”, // space separated list of category IDs — leave empty to get all
$order = ‘DESC’, // either ‘ASC’ (ascending) or ‘DESC’ (descending)
$offset = 0, // number of posts to skip
$date_format = ‘m/d/Y h:i a’, // Date format, php-style, if different from blog’s date-format setting
$authors = ”, // space separated list of author IDs — leave empty to get all
$include_passworded_posts = false)
{
return c2c_get_recent_posts($num_posts, $format, $categories, ‘max_comment_date’, $order, $offset, $date_format, $authors, $include_passworded_posts);
} //end function get_recently_commented()function c2c_get_recently_modified ($num_posts = 5,
$format = ”“,
$categories = ”, // space separated list of category IDs — leave empty to get all
$order = ‘DESC’, // either ‘ASC’ (ascending) or ‘DESC’ (descending)
$offset = 0, // number of posts to skip
$date_format = ‘m/d/Y’, // Date format, php-style, if different from blog’s date-format setting
$authors = ”, // space separated list of author IDs — leave empty to get all
$include_passworded_posts = false)
{
return c2c_get_recent_posts($num_posts, $format, $categories, ‘modified’, $order, $offset, $date_format, $authors, $include_passworded_posts);
} //end function c2c_get_recently_modified()//
// ************************ END TEMPLATE TAGS ********************************************************************
//function c2c_comment_count ($post_id) {
global $wpdb, $tablecomments;
if (!isset($tablecomments)) $tablecomments = $wpdb->comments;
return $wpdb->get_var(“SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = ‘$post_id'”);
} //end function c2c_comment_count()function c2c_get_recent_tagmap ($posts, $format, $tags, $date_format) {
if (!$tags) return $format;
global $authordata, $post;//– Some things you might want to configure —–
$excerpt_words = 6; // Number of words to use for %post_excerpt_short%
$excerpt_length = 50; // Number of characters to use for %post_excerpt_short%, only used if $excerpt_words is 0
$idmode = ‘nickname’; // how to present post author name
$comment_fancy = array(‘No comments’, ‘1 Comment’, ‘%comments_count% Comments’);
//– END configuration section —–if (!$date_format) $date_format = get_settings(‘date_format’);
// Now process the posts
$orig_post = $post; $orig_authordata = $authordata;
foreach ($posts as $post) {
$text = $format;
$comment_count = ”;
$authordata = ”;
$title = ”;// If want last_comment information, then need to make a special db request
$using_last_comment = 0;
foreach ($tags as $tag) {
if (strpos($tag, ‘last_comment’) !== false) { $using_last_comment = 1; break; }
}
if ($using_last_comment) {
global $wpdb, $tablecomments, $comment;
if (!isset($tablecomments)) $tablecomments = $wpdb->comments;
$comment = $wpdb->get_row(“SELECT * FROM $tablecomments WHERE comment_post_ID = ‘$post->ID’ AND comment_approved = ‘1’ ORDER BY comment_date DESC LIMIT 1”);
}// Perform percent substitutions
foreach ($tags as $tag) {
switch ($tag) {
case ‘%comments_count%’:
if (!$comment_count) { $comment_count = c2c_comment_count($post->ID); }
$new = $comment_count;
break;
case ‘%comments_fancy%’:
if (!$comment_count) { $comment_count = c2c_comment_count($post->ID); }
if ($comment_count < 2) $new = $comment_fancy[$comment_count];
else $new = str_replace(‘%comments_count%’, $comment_count, $comment_fancy[2]);
break;
case ‘%comments_url%’:
$new = get_permalink() . “#postcomment”;
break;
case ‘%comments_URL%’:
if (!$title) { $title = the_title(”, ”, false); }
$new = ‘‘.$title.’‘;
break;
case ‘%last_comment_date%’:
$new = mysql2date($date_format, $comment->comment_date);
break;
case ‘%last_comment_id%’:
$new = $comment->comment_ID;
break;
case ‘%last_comment_url%’:
$new = get_permalink().’#comment-‘.$comment->comment_ID;
break;
case ‘%last_commenter%’:
$new = apply_filters(‘comment_author’, $comment->comment_author);
break;
case ‘%last_commenter_URL%’:
$author = apply_filters(‘comment_author’, $comment->comment_author);
$new = ‘comment_author_url).'” title=”Visit ‘.$author.’\’s site”>’.$author.’‘;
break;
case ‘%post_author%’:
if (!$authordata) { $authordata = get_userdata($post->post_author); }
$new = the_author($idmode, false);
break;
case ‘%post_author_count%’:
$new = get_usernumposts($post->post_author);
break;
case ‘%post_author_posts%’:
if (!$authordata) { $authordata = get_userdata($post->post_author); }
$new = ‘ID, $authordata->user_nicename).'” title=”‘;
$new .= sprintf(__(“Posts by %s”), htmlspecialchars(the_author($idmode, false))).'”>’.stripslashes(the_author($idmode, false)).’‘;
break;
case ‘%post_author_url%’:
if (!$authordata) { $authordata = get_userdata($post->post_author); }
if ($authordata->user_url)
$new = ‘user_url.'” title=”Visit ‘.the_author($idmode, false).’\’s site”>’.the_author($idmode, false).’‘;
else
$new = the_author($idmode, false);
break;
case ‘%post_content%’:
$new = apply_filters(‘the_content’, $post->post_content);
break;
case ‘%post_date%’:
$new = mysql2date($date_format, $post->post_date);
break;
case ‘%post_excerpt%’:
$new = apply_filters(‘the_excerpt’, get_the_excerpt());
break;
case ‘%post_excerpt_short%’:
$new = ltrim(strip_tags(apply_filters(‘the_excerpt’, get_the_excerpt())));
if ($excerpt_words) {
$words = explode(” “, $new);
$new = join(” “, array_slice($words, 0, $excerpt_words));
if (count($words) > $excerpt_words) $new .= “…”;
} elseif ($excerpt_length) {
if (strlen($new) > $excerpt_length) $new = substr($new,0,$excerpt_length) . “…”;
}
break;
case ‘%post_id%’:
$new = $post->ID;
break;
case ‘%post_modified%’:
$new = mysql2date($date_format, $post->post_modified);
break;
case ‘%post_title%’:
if (!$title) { $title = the_title(”, ”, false); }
$new = $title;
break;
case ‘%post_url%’:
$new = get_permalink();
break;
case ‘%post_URL%’:
if (!$title) { $title = the_title(”, ”, false); }
$new = $new = ‘‘.$title.’‘;
break;
}
$text = str_replace($tag, $new, $text);
}
echo $text . “\n”;
}
$post = $orig_post; $authordata = $orig_authordata;
return;
} // end function c2c_get_recent_tagmap()function c2c_get_recent_handler ($posts, $format = ”, $date_format = ”) {
if (!$format) { return $posts; }// Determine the format of the listing
$percent_tags = array(
“%comments_count%”, // Number of comments for post
“%comments_fancy%”, // Fancy reporting of comments: (see get_recent_tagmap())
“%comments_url%”, // URL to top of comments section for post
“%comments_URL%”, // Post title linked to the top of the comments section on post’s permalink page
“%last_comment_date%”, // Date of last comment for post
“%last_comment_id%”, // ID for last comment for post
“%last_comment_URL%”, // URL to most recent comment for post
“%last_commenter%”, // Author of last comment for post
“%last_commenter_URL%”, // Linked (if author URL provided) of author of last comment for post
“%post_author%”, // Author for post
“%post_author_count%”, // Number of posts made by post author
“%post_author_posts%”, // Link to page of all of post author’s posts
“%post_author_url%”, // Linked (if URL provided) name of post author
“%post_content%”, // Full content of the post
“%post_date%”, // Date for post
“%post_excerpt%”, // Excerpt for post
“%post_excerpt_short%”, // Customizably shorter excerpt, suitable for sidebar usage
“%post_id%”, // ID for post
“%post_modified%”, // Last modified date for post
“%post_title%”, // Title for post
“%post_url%”, // URL for post
“%post_URL%”, // Post title linked to post’s permalink page
);
$ptags = array();
foreach ($percent_tags as $tag) { if (strpos($format, $tag) !== false) $ptags[] = $tag; }
return c2c_get_recent_tagmap($posts, $format, $ptags, $date_format);
} //end function c2c_get_recent_handler()?>
I’ve got a problem in modifyng the plugin with your instructions. In fact using your code I’ve got this error from MySQL:
” Unknown column ‘wp_term_relationships.term_taxonomy_id’ in ‘on clause’] ”
because you put the 2 LEFT JOIN in a bad order.
The LEFT JOIN correct syntax isSELECT fields FROM first_table LEFT JOIN second_table ON first_table.primary_key = second_table_external_key
And so the correct LINE 59 is:
if ($categories) {
$sql .= "LEFT JOIN $tableterm_relationships ON ($tableposts.ID = $tableterm_relationships.object_id) LEFT JOIN $tableterm_taxonomy ON ($tableterm_relationships.term_taxonomy_id = $tableterm_taxonomy.term_taxonomy_id)";
$cats = explode(' ', $categories);
}Everything works correctly with this line for me.
- The topic ‘Customizable Post Listings – not able to limit category listing’ is closed to new replies.