Get page content by category or tag

I’m trying to sort of emulate the Views behaviour in Drupal with WordPress, but doing it in theme templates.

Now, I have pages that are assembled by using wp_query in the template to get posts belonging to a certain category.

Read More

But now I also want to be able to create another page which performs a query to get all the pages above, but only the content actually in those pages, not the content in the posts within them. Taking this a step further, I would actually have wanted to be able to refer to specific fields within the target page and display them. The end result should be a sort of post list on this page with the titles and introductions of each of the “assembled” pages.

I should mention that I am quite new to both Drupal and WordPress, and am pretty much testing them both out to try to decide what suits my site best (not a blog!) So far I have liked the Views and Panels approach of Drupal, but in most other aspects WordPress has seemed more suitable.

But how would one go about doing the above task in WordPress?

I have managed to get the desired result by doing this (at least getting the page in question, and only the content in it, not the posts. But I cannot select more granular information though):

$my_query = new WP_Query( array(
    'post_type' => 'page',
    'post__in'  => array( 76, 151 )
) );

But that means having to specify particular page ids, which is undesirable. I want it to be possible for users to add pages to be included in the summary page just by adding a tag or category on them. I have not been able to manage to use categories or tags to select the same content as above though (even though I have plugins installed to allow both on pages).

Related posts

Leave a Reply

1 comment

  1. It sounds like you really need to be using posts for your content rather than pages since you want to organize your content into categories. You can use a page to display a custom page template (the Template Name: comment as shown below defines a new page template; that template will then be available as a in a dropdown near the page “publish” button). This template can show metadata from posts in a particular category by using code similar to this:

    <?php
     /*
    Template Name: My Page Template
    */
    ?>
    <?php get_header(); ?>
    
    <h3>My Posts in My Category</h3>
    
       <?php 
       query_posts('category_name=my-category-slug-name&showposts=3'); ?>
    
       while (have_posts()) : the_post(); 
          thumb = get_post_meta($post->ID, "promo_image-215x150", true);
    
          if ($thumb != "")  { ?> 
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
              <img src="<?php $values = get_post_custom_values("promo_image");
              echo $values[0]; ?>" alt="<?php the_title(); ?>"  />
            </a>
    
         <?php 
         } else { ?>
           <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title (); ?></a>
         <?php } ?>
    
      <?php endwhile; ?>