• Resolved svedish

    (@svedish)


    Hi guys,

    just a small thing that I figured out while working on a custom query. I started my code like this:

    <?php $my_query = new WP_Query('post_type = london_hotel');
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

    with a space between the equal sign and both post_type and london_hotel. It didn’t work. It worked only when I removed the space. There is no mention to this in the documentation. Now, is it something I’m missing here? PHP in general doesn’t care if you have a space or not between a term and an equal sign.

    Thanks in advance for any help.

Viewing 5 replies - 16 through 20 (of 20 total)
  • Yes – that works for me.

    Thread Starter svedish

    (@svedish)

    That’s incredible…Can you please send me the WP_Query code you have used? I’ll install a plain version of WordPress and I’ll try it with no code hacks and plugins installed.

    Thanks.

    Thread Starter svedish

    (@svedish)

    Hi Esmi,

    I have reinstalled WP 3.1.3 from scratch. Plain vanilla…

    ??I have created two more posts (Post two and Post three)
    ??I have then created three pages: Page one, Page two and Page three.
    ? I have created a page called Page list that use a template called ‘Page listing’.
    ??Inside this template I have put this code:

    <ul>
    <?php $my_query = new WP_Query('post_type=page');
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    </ul>

    The Page list page displays, correctly, a list of all available pages

    * Page list
    * Page three
    * Page two
    * Page one
    * Sample Page

    IF I add a SPACE before and after the equal sign of the WP_Query argument

    new WP_Query('post_type = page');

    the list displays:

    * Post three
    * Post two
    * Hello world!

    Meaning that the loop is falling back to listing the posts available, not the pages (i.e. posts of post_type page).

    Sorry, I don’t know what you have done, but I can confirm this doesn’t seem to work as I’d expect.

    Thread Starter svedish

    (@svedish)

    I wonder if I should open a bug report for this? Is anyone else able to reproduce this behavior?

    Thanks. ??

    Thread Starter svedish

    (@svedish)

    Hi all,

    Ok, I have joined the IRC channel of WordPress and I got an answer that sounds conclusive, so I’ll update this post in case someone else needs to find out the answer to these questions.

    As far as they told me it is NOT allowed to use spaces in that form of query passing. That is why WP_Query doesn’t behave as expected when spaces are used.

    Another good way to write the query is by using an array notation like this:

    WP_Query(array('post_type' => 'london_hotel'));

    Hope this will help someone else as well.

    Take care

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘WP_Query no space in arguments?’ is closed to new replies.