Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've spent a few days researching working with WP static pages - as separate from posts.

Although I've found useful guidance, I can't seem to find any good tutorials or worked examples that show me how to work with pages using a modified WordPress Loop that will enable me to filter on specific page criteria and extract relevant content.

I am creating a site that will be used annually for events. I want to have a page type (or custom post type) for Calls to Action. My expectation is that there will only ever be a limited number of call to action pages: typically only a few, and likely to be no more than 10 for the site.

I'd like the home page to list a summary of the content on these pages, and build that using a loop: but I want to filter this loop by both the specific page content type (e.g. CallToAction), as well as a status indicator, such as "sticky" or active.

I'd like to either interrogate the page by its template, or use some other indicator such as a custom field that I check.

Being able to set the desired order of the content pages, and sorting their appearance by that order is also a requirement, and I'd like these pages to be shown in the site menu: probably under a parent menu of Calls To Action.

Due to the limited number of pages and the relatively static nature of them, static pages feel like a good approach, however my reading so far indicates that what I want to do with the loop control could be challenging, maybe impossible.

My main question is can I interrogate a page to find out about template type or custom meta data within a loop?

A custom post type seems like a solution that will work, but having a separate post type seems overkill.

Suggestions?

[Edited 14 May 2013 to simplify/ clarify]

share|improve this question

2 Answers 2

I would create an "Events" custom post type and then create a "Call to Action" custom taxonomy and associate this taxonomy with the custom post type.

This way you can have an unlimited amount of events which can be associated with taxonomy terms (your Call to Action). This makes much more sense since you project that you will have few Call to Action types (or terms).

You can the query posts by post type as well as taxonomy, additionally you can also query by meta key (should you attach custom fields to your post type for more definition).

Focus your attention on this Codex page here:

http://codex.wordpress.org/Class_Reference/WP_Query

...to learn more about how to query posts, by type, taxonomy and meta; not to mention much more.

Here is an example of setting up a basic query to query by your specified post type and taxonomy.

$args = array(
    'post_type' => 'events', //this is a custom post type
    'tax_query' => array(
        array(
            'taxonomy' => 'calltoaction', //this is a custom taxonomy
            'field' => 'slug',
            'terms' => 'somecalltoactionterm' //a term within your taxonomy
        )
    )
);
$query = new WP_Query( $args );

You could set these parameters dynamically through a form using $_GET if you wanted to create a filter that responded to a user changing values in say a dropdown menu or via checkboxes for examples.

Relevant reading:

share|improve this answer
    
Thanks @userabuser - appreciate your reply. I have done some work already with Custom Post Types and used them in loops, so I'm pretty comfortable with that approach. My main concern was in understanding whether there was an alternative, possibly better approach using static pages, and whether there were similar ways to query and work with static pages. Are static pages a possibility in the case I have outlined? Are they problematic in important ways that custom post types aren't? –  Paul Zee May 7 '13 at 8:18
    
Not necessarily problematic, no. In fact pages, are a form of post type themselves, they are one of the two default, in built posts type, the other being posts. So a custom post type is just an extension of that and for what its worth in situations where you need to separate your concerns (data) a custom post type makes a lot of sense and neither makes it more or less difficult than using any of the former. So in your instance, from what I understand, I would have a separate post type events and accompanying taxonomy to boot. This offers more flexibility going forward. –  userabuser May 7 '13 at 8:29
    
You can query and work with any of the post types, inbuilt or custom, individually or collectively and further define your queries with category, taxonomy or meta parameters and much more as shown in the WP_Query documentation. That there pretty much lays out what you can do, although beyond that you can write your own SQL too depending upon how complex you wish to get but for your purpose WP_Query will provide you all the flexibility you need. –  userabuser May 7 '13 at 8:35
up vote 0 down vote accepted

@userabuser's input provides a useful discussion on using Custom Post Types to solve the customisation need. However, this still left unanswered my main question:

Can I interrogate a pages template type or custom meta data within a loop?

I wanted to use wordpress static pages with a custom template as an alternative to Custom Post Types. For the small number of pages involved in this case, Custom Post Types really seem like overkill, and clutter up the WordPress menu with the extra custom type entries.

I ended up answering this part of my question, and it turned out to be fairly simple to do:

I created a new calls-to-action.php file to process the pages, and a pages_call-to-action.php template file:

<?php

$callstoaction = get_pages(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page_call-to-action.php',
    'post_status' => 'publish,private',
    'hierarchical' => 0,
    'sort_column' => 'menu_order',
    'sort_order' => 'ASC'

);
?>

followed by a loop as follows:

<?php foreach($callstoaction as $i => $calltoaction) : ?>

     // Do some stuff

<?php endforeach; ?>

Note the meta_key and meta_value pair that identify the template attribute of the page.

This enables you to create standard wordpress pages, set their template through the standard wordpress UI to be "Call To Action" (page_call-to-action.php), and then be able to handle them specifically in a page processing loop as I described above, avoiding the need for a custom post type.

For reference, here's the Template file:

<?php
/**
 * Template Name: Call To Action
 * Description: Provides a basic template for call-to-action pages.
 */

get_header(); the_post(); ?>

<div id="staticpage-single" <?php post_class() ?>>
     // template layout goes here
</div>

<?php get_footer(); ?>

The only real downside I've seen so far in considering templated static pages vs Custom Post Types is that wordpress static pages "are not included in your site's feed" (as noted in the Codex link below), however Hesham Zebida notes (see link below) that there is a "rss-includes-pages" plugin available to address that problem :)

See also:

share|improve this answer
    
FYI, you can see the results at euriskomelbourne.com, where the calls to action are shown on the home page (Contribute, Support, Participate) –  Paul Zee May 14 '13 at 10:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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