Support » Fixing WordPress » WP_Query no space in arguments?

  • Resolved 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 15 replies - 1 through 15 (of 20 total)
  • The space between the equals sign and post_type and london_hotel won’t make the slightest difference.

    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:

    <?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(); ?>

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


    <?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?

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

    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

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

    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?

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

    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.

    That would be Unicode (UTF-8).

    Try saving it in ANSII format

    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.

    Try using TextWrangler.

    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.

    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.

    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.

    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!

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