Archive for November, 2008

Using WP_Query to create custom queries

while reading my usual RSS feeds I stumbled upon this article on displaying your latest post.

It took me eye as I have been busy implementing similar things for my upcoming website, except I have been using WordPress’ WP_Query function to achieve the same results.

Basically WP_Query is used to create a seperate mini-loop outside of your main loop.  It has many advantages over using a custom SQL query like

  1. You don’t need to know proper SQL syntax
  2. It’s a core WordPress function, so if the database structure changes it will still work
  3. is more concise and uses less code
  4. highly customisable in that you can easy modify the post parameters (specify category, author, sort order, etc.)

The below is just a simple demo the grabs a link to the latest post.  It shows how easy WP_Query really is to use.


< ?php

$my_query = new WP_Query('showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
echo '<a href="' . get_permalink() . '">' . the_title('','',FALSE) . '</a>';
endwhile;

?>

(2 votes, average: 5 out of 5) Loading ... Loading ... Comments



Subscription

RSS

Email:

Tag Cloud

7 11 ACA Accident Accidents Advertisements Airport Americans Animals Army article Australian Babies Bacon Badminton Balloons Balls Banana Banks Banksy Basketball batman BBQ Beer bike Bikes Blender Blonde Blondes Bollywood Bono Borat Boss Bowling Boxing Britney Spears Bush Caratoons Cars Cartoons Cats Celine Dion Chair Charities Chaser Chat Cheerleader Cheese Chicken Children Christmas Chuck Norris Cinder Blocks Coffee Comedy Communication Computer Computer Games Computers Cooking Cops Costumes Cows Crazy Criminals Crocodiles Crooks Crows Cycling Dacning Dancing Danger Mouse Death Dinosaurs Disco Doctor Dogs Drinking Drugs Drunk Election Ellen Emo Exercise Explosions Eye Chart facebook Faces Fail Family Guy Fancy Dress Farmer Farting Feminism Fight Fights Finland Fire Firefighters Fireworks Food Football Gameshow Gameshows Geek german Glowsticks God Golf Golf Marriage Google Goonies Goths Graffiti Greek Grillz guitar hero Guns Gym Hancock Hands Hecklers heroes Hitler Hospital House Hunting Husbands Hyphenation Ideol Idiots Idol IKEA Illness Images Indian Infidelity Infomercial Internet Irish Jackass Jackie Chan Japan Jeans Jesus Jewellery Jokes Jokes Lawyers Juggling Karaoke Karate Kasou Kids Kobe Bryant Kwik E Mart lego Letters Life Lionel Richie lolcat Lollies LOTR Magic Management Mario Marriage Marrige Maths Meat Men Mexican Michael Jackson Midget Mime Mixtaping Mobile Phones Models Monkeys Monster Trucks Moonwalk Movies Muppets Muscles Music Music Video Musical Musicians Nerd Nerds New York New Zealand News Nintendo nurse Office old men Old People Oldies Origami Pancakes Paris Hilton Parkour Parody Party Pets Photoes Pi Pickles Pictures Pictures Cars Pizza Plane Plugin PMS Police Politics Pool Powerpoint Prank Pranks Preaching Priest Prison Quiz Rainbow Rap Rape Rapping Real Stories Rednecks Reggae Rick Rolld Riding Robot Russia Salad Fingers Science Sesame Street Sewing Sex sheep Shopping Simpsons Singing skateboard Skating skinny dipping Skunk Slide Slow Motion Smoking Soccer social networking south park Spelling Sport Sports Squirrel Stand up Star Trek Star Wars Stock Market Stunts Stupidity Superman SWAT swearing Sweded swimming Talent Show Tattoo Tech Support Television Terrorists Thiefs TMNT to the face Toilet Tools Trains Transformers Unicorn USA Vegetables video games Videos Vidoes Wall Washing WaterSlide Webcam Weird Weird Al Wife Wifes Willy Wonka Windows Women WordPress Workout WP LightForm Wrestling Yoda Zorba

Top Commenters

  • Alex (44)
  • little duck (22)
  • jfed (11)
  • keiser (8)
  • Tom @ halloween witch costume (7)
  • David Potter (7)
  • greg @ skunk costume (7)
  • Rochelle @ Fairy Princess Costumes (6)
  • doug @ Kids Vampire Costumes (6)
  • Terry @ Movie Character Costumes (5)