deadlyhifi’s posterous

i like bikes, punk rock, ska, reggae, Wordpress, PHP, and am a hypocrite. 

Separate Posts and Pages in Wordpress Search Results

I recently ran into a problem with Wordpress search. The search results page (search.php) 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>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; ?>


Loading mentions Retweet
Filed under  //   query_posts   search.php   the loop   wordpress  

Comments [0]

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

Loading mentions Retweet

Comments [0]

A Google Chrome advert at Bradford train station?!

Loading mentions Retweet

Comments [0]

Music

Loading mentions Retweet

Comments [0]

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, as defined in flickr.photos.search.

This 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 xmlToArray: codepad.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>Recent Uploads tagged alturatrail</title>


<link>http://www.flickr.com/photos/tags/alturatrail/</link>


 <description></description>


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


<image>


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


<title>Recent Uploads tagged alturatrail</title>


<link>http://www.flickr.com/photos/tags/alturatrail/</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.

Loading mentions Retweet

Comments [0]

Dyson motor #FAIL

Loading mentions Retweet

Comments [0]

Google Chrome comedy warning

Loading mentions Retweet

Comments [0]

Bike with strap-on engine (fingerbanged)

Loading mentions Retweet

Comments [0]

Direct Mount Front Mech

     
Click here to download:
Direct_Mount_Front_Mech.zip (513 KB)

Loading mentions Retweet

Comments [0]

How Bizarre

I went to a gig last night, Chuck Ragan, 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.

Loading mentions Retweet

Comments [0]