WordPress.org

Support

Support » Themes and Templates » query_posts $pagename variable not working if page uses “&” symbol

query_posts $pagename variable not working if page uses “&” symbol

  • funkboybilly

    @funkboybilly

    I’m using the $pagename variable in a query_posts string to pull subpages of defined page:

    <?php
    $pagename = 'Products & Services';
    $subpage_query = new WP_Query();
    $subpage_query->query("pagename=$pagename");
    ?>

    I’m using this instead of wp_list_pages because I need to customise the output of the code. I’ve only included the query portion of the code for simplicity.

    The problem is that when a page name contains a symbol such as the example above using “&”, the connection breaks. If I leave out the “&” symbol, all pages are listed, and if I type the name of a page that doesn’t exist, no pages are listed.

    How do I get around this if a page title contains a symbol?

Viewing 2 replies - 1 through 2 (of 2 total)
  • tugbucket

    @tugbucket

    your actual page name is “products-services”

    Thanks, you’re right. WordPress documentation says that “pagename” is the page’s path, not it’s title.

    Is there a way to query posts/pages using a page’s title? Or is there a way to find a page’s slug/ID based on it’s title and then use that to query posts?

    *the result I’m looking for is to write the title of any page into a custom field. WordPress finds the page from the custom field page title, and then lists it’s subpages.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query_posts $pagename variable not working if page uses “&” symbol’ is closed to new replies.
Skip to toolbar