• Resolved pmdci

    (@pmdci)


    Hi there,

    I am having an issue with query_posts that is driving me crazy! I am in desperate need of help ??

    Here is what I am trying to do with my single.php file:

    1. single.php should only return posts from the type “post” (the default that comes with WordPress). This is because I created other type pof posts, which I will be using custom page templates for these. I would also like these posts to be returned in random order. So this is how my query_posts() was set out:

    query_posts('post_type=post&orderby=rand');

    OK, easy peasy so far so good. But now here is where my nightmare started: I also needed an additional conditional so only posts with the tag featured would appear in the frontpage. So my query_posts() would be almost the same as the above with the exception of an additional argument:

    query_posts('post_type=post&orderby=rand&tag=featured');

    So, all I did is to add &tag=featured as the last argument.

    The problem is trying to cobine these two on the same page! I am pulling my hair off here… I read hundred of different posts and blogs about the_loop and query_posts, but most of them seems to require having to re-write the entire output of <?php if (have_posts()) : while (have_posts()) : the_post(); ?>. And this sure looks like an overkill.

    Here is what I tried to do, but it doesn’t work. I think that by looking at the code you PHP/WordPress GURUS out there would understand pretty quick what is that I am trying to do. But this is how far my moderate PHP skills got me so far…

    <?php /** DEFAULT query_posts() **/
             query_posts('post_type=post&orderby=rand');
    ?>
    
    <?php /** query_posts() for is_home() **/
    if (is_home()) {
             query_posts($query_string . '&tag=featured&post_type=post&orderby=rand');
    }
    ?>

    Basically what happens is that the query_post() inside is_home() takes precedence. So when I go to categories containing posts without the featured tag, still it only shows posts with the featured tag.

    Could someone please give me a hand with this? I will really appreciate the help!

    Thanks in advance,
    P.

Viewing 7 replies - 1 through 7 (of 7 total)
  • popper

    (@julialasarte)

    Hey there pmdci,

    Your code worked perfectly fine when I tested it:

    <?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');
    }
    ?>

    It showed me the featured posts in the home page, an on the other pages random posts. that’s what you want it to do, right?

    Thread Starter pmdci

    (@pmdci)

    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.

    popper

    (@julialasarte)

    Ah, I think I got your problem now. Can you paste your index.php, on wordpress.pastebin.com so I see your code and edit there?

    Thread Starter pmdci

    (@pmdci)

    Hi Julia!

    Thank you for your prompt reply!

    I’ll be happy to. Here is the link: https://wordpress.pastebin.com/6vxQFHQc

    NOTE: I am new to pastbin. First time using it, but I sent the link to a friend and he managed to open it and see the code. Not sure what would be the next steps though ??

    A thousand thanks for your help!

    Best Regards,
    P.

    Thread Starter pmdci

    (@pmdci)

    By the way,

    Since I am not a GURU of PHP/WordPress, I think t is worth mentioning how I managed to play around with custom post types, taxonomies and custom fields (so to avoid any confusions ?? )

    For the creation of my custom post type partner and my custom taxonomy city, I am using a plug-in called GD Custom Posts And Taxonomies Tools by Milan Petrovic (I highly recommend it it).

    I am also using VerveMeta Boxes so I can create custom fields for specific post types.

    Cheers,
    P.

    popper

    (@julialasarte)

    Thread Starter pmdci

    (@pmdci)

    Julia,

    IT IS WORKING! Unbelievable… I did all the possible UATs, but it is working exactly as expected.

    You have my sincere thanks.

    As I promised, I will be posting a nice dummy tutorial on this case for the query_posts() function here at www.remarpro.com. I just need to finish this site because I am two days behind the schedule (meaning: no donuts for me).

    Regards,
    P.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘using query_posts in single.php with is_home IF/ELSE’ is closed to new replies.