query_posts $pagename variable not working if page uses "&" symbol (3 posts)

  1. funkboybilly
    Posted 6 years ago #

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

    $pagename = 'Products & Services';
    $subpage_query = new WP_Query();

    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?

  2. tugbucket
    Posted 6 years ago #

    your actual page name is "products-services"

  3. funkboybilly
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.