WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP_Query no space in arguments? (21 posts)

  1. svedish
    Member
    Posted 3 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    The space between the equals sign and post_type and london_hotel won't make the slightest difference.

  3. svedish
    Member
    Posted 3 years ago #

    Hi Esmi,

    Thanks for your answer, but I am trying again right now. If I add spaces between the equal sign and both terms to the left and right the code doesn't display the list of post items anymore (it just displays one list item and the one URL with no anchor text that points to the first post in the blog http://MYDOMAIN/cms_tests/wordpress/?p=1). The code is the following:

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

    When I remove the spaces (OR if I remove JUST the space after post_type)

    <?php $my_query = new WP_Query('post_type=london_hotel');

    or

    <?php $my_query = new WP_Query('post_type= london_hotel');

    it works and both the titles of the posts are listed as links (I have two posts of type london_hotel). By the way if you see the_field('hotel_name' it is because I'm using Advanced Custom Fields).

    Weird...What do you think?

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    I think the issue is elsewhere in your code. PHP is not space sensitive.

  5. svedish
    Member
    Posted 3 years ago #

    Hi Esmi,

    In fact, PHP is not space sensitive. That's why I was so surprised!

    You mean the code of the same file? It's really not that long because it's just a test.

    It's just a separate template (* Template Name: Hotels listing) that displays a list of posts of that custom type. Nothing else.

    Just get header, do this loop, close the query and get the footer.

    Weird... :S

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    What are you using to edit this file? What encoding are you using when saving it?

  7. svedish
    Member
    Posted 3 years ago #

    I can use whatever. I was using the FTP app text editor (Transmit), but I just copied the whole code into BBedit and created a brand new file and all the problem persists. By the way, I have used the FTP editor for years to edit any sort of php file, inside and outside WordPress, so I doubt that could be the problem. Am I spotting a bug here? Is it worth escalating the thing?

  8. esmi
    Forum Moderator
    Posted 3 years ago #

    It's not a WordPress bug. What encoding are you using when saving the file?

  9. svedish
    Member
    Posted 3 years ago #

    I've created another page and template which is identical to the other but have two spaces around that equal sign. They both list (should list) the posts of post type london_hotel. Only the one with no spaces works.

  10. svedish
    Member
    Posted 3 years ago #

    That would be Unicode (UTF-8).

  11. esmi
    Forum Moderator
    Posted 3 years ago #

    Try saving it in ANSII format

  12. svedish
    Member
    Posted 3 years ago #

    Sorry Esmi,

    ANSII is not even available in BBedit as a "Save as" option. To be honest I highly doubt the problem is the encoding... But I am happy to give it a shot, if I only knew how to save the file as ANSII. I'm on a Mac.

  13. esmi
    Forum Moderator
    Posted 3 years ago #

    Try using TextWrangler.

  14. svedish
    Member
    Posted 3 years ago #

    TextWrangler is the Free version of BBedit, so all that can be done with TextWrangler can be done in BBedit.

    Also, and of course this is NO conclusive proof, in the WP_Query manual page, in all examples that make use of an equal sign there is no use of spaces, never. http://codex.wordpress.org/Class_Reference/WP_Query

    This makes me thing that maybe that piece of code gets appended to some query and possibly doesn't like spaces? Just a random thought from a non expert...I really can't figure out why this happens, but I doubt it's a question of encoding to be honest.

  15. esmi
    Forum Moderator
    Posted 3 years ago #

    this is NO conclusive proof, in the WP_Query manual page, in all examples that make use of an equal sign there is no use of spaces, never.

    I use spaces all of the time (for better readability) and I've never had a problem.

  16. svedish
    Member
    Posted 3 years ago #

    I also use spaces all the time in PHP for better readability.

    Have you tried to reproduce my example using spaces in the WP_Query - i.e. WP_Query('page_id = 3') instead of WP_Query('page_id=3')?

    Thanks for helping!

  17. esmi
    Forum Moderator
    Posted 3 years ago #

    Yes - that works for me.

  18. svedish
    Member
    Posted 3 years ago #

    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.

  19. svedish
    Member
    Posted 3 years ago #

    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.

  20. svedish
    Member
    Posted 3 years ago #

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

    Thanks. :)

  21. svedish
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic