Hi!
Thank you for your reply!
Well for me it doesn’t work… ??
Let me tell you what I found so far. What annoys me the most is that I KNOW for sure that it is all going wrong due to my about-average knowledge of PHP/WordPress. I am pretty sure someone that knows WordPress inside out would realise my mistake with a blink of an eye.
JUST A SIDE NOTE (if I may, feel free to skip it): If I manage to get the solution for this problem I am having, I promisse hand on hard that I will post a tutorial based on my real-life example in quite simple “dummy” terms in the Codex, so it can be of help to people out there (I am sure that I am not alone) with the same or similar issues that I am having, or trying to accomplish something similar. Thing is some of these tutorials are quite hard to follow to the less-advanced PHP/WordPress wannabes (particularly when you have all these functions and codes like query_posts()
, the_loop
, get_posts()
, wp_query()
and so on). It would also be a great learning experience for me to write a tutorial anyway ?? so I would be thrilled to do it!)
So if you read so far, thank you for helping me with my plead. Here is the business:
BACKGROUND
I think it is important to say that besides the default post type that comes with WordPress (simply called post), which I am trying to use here as an argument for the query_posts()
, I have created another post type called “partners”.
I also used the default categories for property types (eg: houses, flats, etc) and I created a custom taxonomy for cities (eg: New York, Madrid, etc)
The whole ideas is that single.php:
1. Only show the default post type that comes with WordPress (called post). I will create a special page template for my custom post type(s).
2. At the HOME PAGE (using the is_home()
conditional), only posts with the featured tag should be displayed.
NOW FOR THE CODE
Basically what I find is that thee ELSE
statement breaks everything.
This code:
<?php
//home
if (is_home()) {
query_posts($query_string . '&tag=featured&post_type=post&orderby=rand');
}
else {
// default
query_posts('post_type=post&orderby=rand');
}
?>
Seems to basically act normal on the home page (based on the is_home()
conditional).
However, if I browse my categories, or the custom taxonomy I created called cities, it simply shows EVERYTHING of the post type post without following the filtering of the categories (e.g.: house, flat, condo, etc), or cities (eg: London, Madrid, New York, etc).
For example, selecting the category Flats from the category dropdown menu would return me EVERY post regardless if it is a flat, house, condo, etc. Selecting the city Madrid would return me EVERY post regardless if it is in Madrid, Chicago, Berlin, etc.
IMPORTANT NOTE: As a matter of fact, some of my categories and cities are empty (no posts associated to them). If I browse them using the code above, I would STILL return me EVERY post.
However, if I remove the ELSE
statement:
<?php
//home
if (is_home()) {
query_posts($query_string . '&tag=featured&post_type=post&orderby=rand');
}
else {
// default - DO NOTHING AS ANOTHER query_posts() breaks everything
}
?>
However this seeems to work as follows:
1. GOOD: The conditional to only show posts with the tag featured on the frontpage is respected.
2. GOOD: Browsing the categories for my posts will only show posts belonging to the appropriate category.
3. GOOD: Browsing the cities (my custom taxonomy) for my posts will only show belonging to the appropriate city.
4. GOOD: Browsing empty cities (my custom taxonomy) and categories, which contain no posts, will return my custom/chick NOT FOUND output.
5. BAD: Browsing the cities (my custom taxonomy) will also show my custom post type partners. This is what I am trying to avoid.
It might don’t look like a big deal (since is 1 bad out of 5), but it is in fact a disaster. I created a custom post type called partners for a reason. They have custom fields and are MEANT to be on a special page with a total different output (by that I mean special CSS code, layouts and so on).
I hope that I managed to give all the information necessary so you guys can kindly give me a hand. As I said before, I will document everything step by step in lame terms, and I plan to do a nice tutorial here in www.remarpro.com based on my real life scenario, so everybody that is on the same page as me can benefit from your help (credits will of course, be given).
Thanks in advance,
P.