deadlyhifi

bikes, web, life, philosophy, hypocrite...

Feb 26

Separate Posts and Pages in Wordpress Search Results

I recently ran into a problem with Wordpress search. The search results would mix pages and posts together and apply the same styling to each, even claiming that pages were posts by assigning a category to them (pages don’t have categories). Searches were also governed by the “Blog pages show at most” option on the Reading Settings page.

I wanted different styling on the pages and posts and to list all search results on one page.

To do this we just run two loops, on the first display only pages, and the second display only posts.

In plain speak:

  • if search returns a result
  • run through all the pages
  • reset query
  • run though all the posts
  • else return an error message

First let’s inform the user what they searched for and how many results were returned.

<h2>Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e('&ldquo;'); echo $key; _e('&rdquo;'); _e(' <small>&mdash; '); echo $count . ' '; _e('results</small>'); wp_reset_query(); ?></h2>

Credit to www.problogdesign.com for that one.

This will return something along the lines of Search Result for “your search term” — 5 results.

Then we run through the loop,

<?php if (have_posts()) : ?>

but we only want to show the pages at this point, plus we want to make sure the results don’t get paged (i.e page 1, page 2 etc.) overriding the default set on our Reader Settings page.

<?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=page'"); ?>

The Wordpress query_posts page shows all the arguments available. We’re interested in the “posts_per_page=-1” and “post_type=page”. ‘-1’ means show all, and ‘page’ means we’re only interested in pages.

Let’s have a title and run through the returned pages, if there are any, and a message if there are no page results.

<h3>&rarr; Pages</h3> <?php if ( $query ) : ?> <?php while (have_posts()) : the_post(); ?> <h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2> <?php endwhile; ?> <?php else : ?> No pages found. <?php endif; ?>

What we just said was, if there are any results list them, else give the error message.

Next we need to reset the query.

<?php wp_reset_query(); ?>

Then we repeat the process but for posts only. Note the post type is now “post_type=post”.

<?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=post'"); ?> <h3>&rarr; Articles</h3> <?php if ( $query ) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post"> <h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2> <span class="author">By: <?php the_author_posts_link(); ?></span> <span class="post-dates"><?php the_time('F jS, Y') ?></span> <span class="post-cat"><?php the_category(', ') ?></span> <div class="entry"> <?php the_excerpt(); ?> <div class="readmore" ><span class="small">Read Full Article &raquo;</span> </div> </div><!-- !entry --> </div><!-- !post --> <?php endwhile; ?> <?php else : ?> No articles found. <?php endif; ?>

If no results were found at all, we’ll give them another error message to close off the “<?php if (have_posts()) : ?>” we started with.

<?php else : ?> <p>No articles found.</p> <?php endif; ?> <?php else : ?> <p>Sorry, your search didn't return any results.</p> <?php endif; ?>

If we put all that together we end up with:

<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e('&ldquo;'); echo $key; _e('&rdquo;'); _e(' <small>&mdash; '); echo $count . ' '; _e('results</small>'); wp_reset_query(); ?></h2> <?php if (have_posts()) : ?> <?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=page'"); ?> <h3>&rarr; Pages</h3> <?php if ( $query ) : ?> <?php while (have_posts()) : the_post(); ?> <h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2> <?php endwhile; ?> <?php else : ?> <p>No pages found.</p> <?php endif; ?> <?php wp_reset_query(); ?> <?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=post'"); ?> <h3>&rarr; Articles</h3> <?php if ( $query ) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post"> <h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2> <span class="author">By: <?php the_author_posts_link(); ?></span> <span class="post-dates"><?php the_time('F jS, Y') ?></span> <span class="post-cat"><?php the_category(', ') ?></span> <div class="entry"> <?php the_excerpt(); ?> <div class="readmore" ><span class="small">Read Full Article &raquo;</span> </div> </div><!-- !entry --> </div><!-- !post --> <?php endwhile; ?> <?php else : ?> <p>No articles found.</p> <?php endif; ?> <?php else : ?> <p>Sorry, your search didn't return any results.</p> <?php endif; ?>

Posted via web from deadlyhifi’s posterous | Comment »


Feb 24

Added a gofasta stripe to my magic mouse so I know where the middle click is.


Dec 21

A Google Chrome advert at Bradford train station?!

Posted via email from deadlyhifi’s posterous | Comment »


Nov 20

Flickr RSS feeds only show latest 20 entries. An API Solution in PHP.

I needed to collect a series of images from Flickr based on a certain tag. RSS yes? No. Flickr RSS return only 20 results. I needed more powers.

After much messing around and searching for other people’s solutions, which I never really found. The following is what I came up with.

First you need to get yourself a Flickr API key. Then using the flickr.photos.search request create the query you want.

e.g. http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=< API_KEY >&user_id=22323629%40N08&auth_token=<  AUTH_TOKEN >&api_sig=< API_SIG >

Note there are plenty of different parameters that can be placed in the query.

The returns the data in XML format. Once we have the XML we need to convert it into an Array. A bit of searching returned the following function, affectionately titled function xmlToArraycodepad.org/5CrZpqeh

This returns a HUGE multidimensional array. Believe me, it’s daunting, which then needs turning into valid RSS, along the same lines as Flickr’s own RSS feeds. Though the query returns fewer parameters.

function arraytorss($xml) {

$rss = ‘<?xml version=”1.0” encoding=”utf-8”?>

<rss version=”2.0” xmlns:media=”http://search.yahoo.com/mrss/” xmlns:dc=”http://purl.org/dc/elements/1.1/” >

<channel>

<title>Give your feed a title</title>

<link>place a link to a Flickr page</link>

  <description></description>

<generator>http://www.flickr.com/</generator>

<image>

<url>http://l.yimg.com/g/images/buddyicon.jpg</url>

<title>Give your feed another title</title>

<link>place a link to a Flickr page</link>

</image>’;

foreach ($xml[‘photos’][0][‘content’][‘photo’] as $key => $name) {

$rss .= ‘<item>’;

$rss .= ‘<title><![CDATA[‘.$name[‘title’].’]]></title>’;

$rss .= ‘<link>http://www.flickr.com/photos/’.$name[‘owner’].’/’.$name[‘id’].’/</link>’;

$rss .= ‘<guid>http://www.flickr.com/photos/’.$name[‘owner’].’/’.$name[‘id’].’/</guid>’;

$rss .= ‘<description><![CDATA[‘;

$rss .= ‘<p>’.$name[‘title’].’</p>’;

$rss .= ‘<p></p>’;

$rss .= ‘]]></description>’;

$rss .= ‘</item>’;

}

$rss .= ‘</channel></rss>’;

return $rss;

}

Feed in your array, and out pops a valid RSS feed. Hopefully you can see how to manipulate the output to suit you needs.

Posted via web from deadlyhifi’s posterous | Comment »


Oct 25

Google Chrome comedy warning

Posted via email from deadlyhifi’s posterous | Comment »


Oct 11

Bike with strap-on engine (fingerbanged)

Posted via email from deadlyhifi’s posterous | Comment »


Aug 28

How Bizarre

I went to a gig last night, Chuck Reagan, in Manchester. Most enjoyable it was. Ben (@ctznsmith) gave me a lift, which was most kind of him. 
On the way home we were discussing our bikes past and present. I spoke of my old Schwinn Powermatic (a BMX bike not a washing machine). Ben said he used to have one of those, with the kink in the down tube.

“Yes, thats the one. I sold it on eBay about 7 years ago. Royal mail lost the pedals and original fork and bars so I had to claim off them and then sent a cheque to the purchaser to make up for it.”
“Really?” says Ben, “I bought my Schwinn off eBay, and the pedals and bars and stuff got lost in the post. And then I got sent a cheque …”

And the realisation happened whilst we drove past my old flat in Kirkstall from where the entire transaction took place. 
Ben was living in Southampton at the time. I only met Ben for the first time last year.
Weird.


here, just before the sale, in all its glory.

Posted via email from deadlyhifi’s posterous | Comment »


Aug 20

Aug 7

CSS shorthand reference sheet

Download now or preview on posterous
cssshorthand.pdf (38 KB)

Posted via email from deadlyhifi’s posterous | Comment »


Jun 28

I did it. I finished. 8:14. I’m going to an all-you-can-eat now. Ace.

Posted via email from deadlyhifi’s posterous | Comment »


Page 1 of 15