<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DQuinn.net &#187; redivider</title>
	<atom:link href="http://www.dquinn.net/tags/redivider/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dquinn.net</link>
	<description>Daniel J. Quinn&#039;s journal of WordPress, electronic publishing, and general geek culture.</description>
	<lastBuildDate>Thu, 22 Jul 2010 23:46:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sort Posts by Last Name Using Nate Olsen&#8217;s WP Snap Plugin for WordPress</title>
		<link>http://www.dquinn.net/sort-posts-by-last-name-using-nate-olsens-wp-snap-plugin-for-wordpress/</link>
		<comments>http://www.dquinn.net/sort-posts-by-last-name-using-nate-olsens-wp-snap-plugin-for-wordpress/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 12:57:39 +0000</pubDate>
		<dc:creator>Daniel Quinn</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[emerson college]]></category>
		<category><![CDATA[fringe magazine]]></category>
		<category><![CDATA[nateomedia]]></category>
		<category><![CDATA[nathan olsen]]></category>
		<category><![CDATA[redivider]]></category>
		<category><![CDATA[sort by last name]]></category>
		<category><![CDATA[wp snap]]></category>

		<guid isPermaLink="false">http://www.dquinn.net/?p=1812</guid>
		<description><![CDATA[I’ve used <a href="http://www.nateomedia.com/">Nateomedia.com</a>’s <a href="http://www.nateomedia.com/wares/downloads/wordpress/wp-snap/">WP Snap plugin</a> for quite awhile now, whether it was to sort through encyclopedia  entries, organize archives of contributor bios, or simulate a dictionary in WordPress. On Nathan Olsen’s website, <a href="http://www.nateomedia.com/wares/downloads/wordpress/wp-snap/?cp=all#comment-14651">one commenter mentioned</a> how he had managed to make WP Snap organize an archive of bios, but sorted by last, middle, and then first name. Unfortunately, the sample code the commenter provided got mangled by Nate’s comments sanitizer, so I decided to muck through the plugin myself and hack together a solution.]]></description>
			<content:encoded><![CDATA[<p>I’ve used <a href="http://www.nateomedia.com">Nateomedia.com</a>’s <a href="http://www.nateomedia.com/wares/downloads/wordpress/wp-snap/">WP Snap plugin</a> for quite awhile now, whether it was to sort through encyclopedia  entries, organize archives of contributor bios, or simulate a dictionary in WordPress. On Nathan Olsen’s website, <a href="http://www.nateomedia.com/wares/downloads/wordpress/wp-snap/?cp=all#comment-14651">one commenter mentioned</a> how he had managed to make WP Snap organize an archive of bios, but sorted by last, middle, and then first name. Unfortunately, the sample code the commenter provided got mangled by Nate’s comments sanitizer, so I decided to muck through the plugin myself and hack together a solution. This is a screenshot of what Nate’s plugin can do, from his website:</p>
<div id="attachment_1789" class="wp-caption aligncenter" style="width: 497px;"><img class="size-full wp-image-1789 " src="http://www.dquinn.net/images/wp-snap_screen.gif" alt="" width="487" /></p>
<p class="wp-caption-text">WP Snap Demo</p>
</div>
<p>I first used <a href="http://www.redividerjournal.org/contributors/">WP Snap on Redivider</a>, the literary journal for Emerson College. But for <a href="http://www.fringemagazine.org/contributors/">Fringe Magazine</a>, I needed to organize contributor bios by last name. And at Harvard Common Press, I needed to do the same thing with <a href="http://www.harvardcommonpress.com/archives/authors/">author bios </a><em>and </em>the <a href="http://www.harvardcommonpress.com/archives/titles/">publisher’s title list</a>. So the first thing I did was cordon off Nate’s original WP Snap function in the plugin, so that I could still sort posts as the plugin normally does:</p>
<p>On line 168, change:</p>
<pre class="brush: php;">$nav = new wp_snap_core();</pre>
<p>To:</p>
<pre class="brush: php;">
if ($snapqueryarray['ordertype'] == &quot;names&quot;) {
    $nav = new wp_snap_core_names();
} else {
    $nav = new wp_snap_core();
}
</pre>
<p>This allows us to make two different calls to the plugin:</p>
<pre class="brush: php;">
// and if we want to sort posts by last, middle, and then first name
&lt;?php  if (function_exists('wp_snap')) { echo wp_snap('&amp;ordertype=names); } ?&gt;

// if we want to sort posts by the title normally:
&lt;?php  if (function_exists('wp_snap')) { echo wp_snap(); } ?&gt;
</pre>
<p>Now all we need to do is install the section wp_snap_core_names() function into the plugin. Search for “$all_posts = $wpdb-&gt;get_results($request);” and insert the following code on the line below it:</p>
<pre class="brush: php;">
for ($i=0; $i &lt; count($all_posts); $i++) {
    $name_set[$i] = preg_replace('/^(' . $exclude[1] . ')+((' . $exclude[2] . ')[\s]+)*|^(' . $exclude[2] . ')[\s]+/i', '', wp_snap_core::accents($all_posts[$i]-&gt;post_title));
}
for ($k=0; $k &lt; count($name_set); $k++) {
    $word_parts = explode(&quot; &quot;, $name_set[$k]);
    $number_of_names = count($word_parts);
    $the_name = &quot;&quot;;
for ($x=$number_of_names; $x&gt;=0; $x--) {
    $the_name .= $word_parts[$x].' ';
}
unset($name_set[$k]);
$name_set[$k] = trim($the_name);
}
</pre>
<p>What we’re doing here is going through the array of post titles and reversing each word in the string, so that the order is last name, first name, and then middle name. This should work for any number of words in the post title.</p>
<p>Now search for this preg_replace, which is just below the above code:</p>
<pre class="brush: php;">
$word_results[$i] = preg_replace('/^(' . $exclude[1] . ')+((' . $exclude[2] . ')[\s]+)*|^(' . $exclude[2] . ')[\s]+/i', '', wp_snap_core::accents($all_posts[$i]-&gt;post_title));
</pre>
<p>Replace this with:</p>
<pre class="brush: php;">$word_results[$i] = $name_set[$i];</pre>
<p>Finally, search for “// Resorts $wp_snap_posttitles, taking excluded words into account” and add the following code just above that line:</p>
<pre class="brush: php;">
for ($i=0; $i&lt;count($all_posts); $i++) {
    $word_parts = explode(&quot; &quot;, $all_posts[$i]-&gt;post_title);
    $number_of_names = count($word_parts);
    $the_name = &quot;&quot;;
for ($x=$number_of_names; $x&gt;=0; $x--) {
    $the_name .= $word_parts[$x].' ';
}
unset($all_posts[$i]-&gt;post_title);
$all_posts[$i]-&gt;post_title = trim($the_name);
}
</pre>
<p>Okay, now the plugin is all set to display these posts sorted by last name first. When you call &lt;?php  if (function_exists('wp_snap')) { echo wp_snap('&amp;ordertype=names); } ?&gt; in your template (following &lt;a&gt;Nate’s regular instructions for using the plugin&lt;/a&gt;), you’ll notice that when you click on a letter, you’ll only get posts where the last name begins with that letter:</p>
<pre class="brush: plain;">
C

Callaway William
Chau François
Cullen Peter
Cummings R. Brian
</pre>
<p>Great, this means the posts are actually being sorted by last name first. The only problem now is that because we altered the post titles in the loop, the names are being <em>displayed</em> last name first, which we don’t want. So we add a bit of code to our template to rearrange the post title back to the original order of the words in the string, so that they display first name first:</p>
<pre class="brush: php;">
&lt;?php  if (function_exists('wp_snap')) { echo wp_snap('cat=4'); } ?&gt;
    &lt;?php $wp_query-&gt;is_archive = true; ?&gt;
    &lt;?php while (have_posts()) : the_post(); ?&gt;
        &lt;?php $author_name = get_the_title(); ?&gt;
        &lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;&lt;?php the_title_attribute(); ?&gt;&quot;&gt;
        &lt;?php // we need to put the names back in the correct order
            $word_parts = explode(&quot; &quot;, get_the_title());
            $the_name = &quot;&quot;;
            $number_of_names = count($word_parts);
            for ($x=$number_of_names; $x&gt;=0; $x--) {
               $the_name .= $word_parts[$x].' ';
            }
            echo trim($the_name); ?&gt;
         &lt;/a&gt;
</pre>
<p>And that’s it. Now our names should display normally: William Callaway, François Chau, Peter Cullen, Brian R. Cummings.</p>
<p>For the sake of posterity (and laziness), you can download version 0.8.6 of WP Snap (the one I tweaked) as well as my modified plugin file below:</p>
<p>Original WP Snap Plugin, version 0.8.6: <a href="http://www.dquinn.net/images/wp-snap.zip">wp-snap.zip</a></p>
<p>Modified WP Snap Plugin: <a href="http://www.dquinn.net/images/wp-snap-dquinn.zip">wp-snap-dquinn.zip</a></p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dquinn.net/sort-posts-by-last-name-using-nate-olsens-wp-snap-plugin-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
