0

I have a series of "Thank You" pages which result from different form submissions. I would like to be able to exclude these from sitemaps based on their template. Specifically, and as an easy example, excluding them from being listed on the basic 404 page which utilizes the wp_list_pages.

I know how to manually exclude by ID but am looking for a better way to do this on scale and a more simplified approach--rather than having to edit code.

Is this approachable from a template perspective (code in template) or from the hook perspective (exclude by template vs exclude by ID). Essentially, the goal is to make this easier for front end content creators so as to avoid having to include multiple people in the process of publishing a single page.

'exclude'      => 'template-name',   versus the standard    'exclude'      => '17',
  • 1
    by template, do you mean a template selected via the metabox when editing a page? – Milo Oct 21 '15 at 20:19
  • I would like content creators to be able to select a page template when editing the page that indicates the page should not be included in the sitemap--that would be ideal. If not that, then perhaps excluding a parent and all children? They could then just be sure to set as a child of specific parent in order to prevent the wp_list_pages from displaying. – Carlos Espinosa Oct 22 '15 at 18:35
0

I would like content creators to be able to select a page template when editing the page that indicates the page should not be included in the sitemap

This seems a bit awkward to me. I would suggest instead to add a meta box that enables content creators to toggle an "include in sitemap" setting for each page. That said, the mechanism for excluding those pages from wp_list_pages would be the same, as the selected template is also stored in post meta data under the key _wp_page_template.

The first step is to get all of the page IDs that satisfy our meta query, whether that be checking for a page template or a flag you set in your meta box handler to denote that page is to be excluded from the sitemap. We also set the fields argument so the returned posts contains only an array of IDs. This will save memory versus querying entire page objects.

$args = array(
    'fields' => 'ids',
    'post_type' => 'page',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => '_hide_from_sitemap',
            'compare' => 'EXISTS',
        )
    )
);
$pages_to_exclude = new WP_Query( $args );

After this is run, $pages_to_exclude->posts will be an array of page IDs that you can pass directly as the exclude argument of wp_list_pages.

  • Thanks Milo...your comments are right on. I've been working on this and have yet to find the solution. I have manually been adding them to be excluded where needed...I think this is a little beyond my scope/understanding of PHP and WP Hooks (fighting with meta baox saving check values, calling that data to the 404 etc., etc. – Carlos Espinosa Oct 30 '15 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.