Title: making a function
Last modified: August 19, 2016

---

# making a function

 *  [xhan](https://wordpress.org/support/users/xhan/)
 * (@xhan)
 * [17 years ago](https://wordpress.org/support/topic/making-a-function/)
 * This code works when I paste it into my template but I can’t work out how to 
   convert it into a function everytime I do it messes up the rest of my template!
 * I’m also pretty sure there’s a cleaner way to do this and suggestions are welcome.
   Basically I want to display 6 pages with the custom field project_active = yes.
 *     ```
       <?php
   
       	 $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
           WHERE wposts.ID = wpostmeta.post_id
           AND wpostmeta.meta_key = 'project_active'
           AND wposts.post_type = 'page'
           ORDER BY wpostmeta.meta_value DESC
        ";
   
       $pageposts = $wpdb->get_results($querystr, OBJECT);
   
       ?>
   
       <ul> 
   
       <?php if ($pageposts): ?>
         <?php foreach ($pageposts as $post): ?>
       	<li><a href="the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
       	<?php endforeach; ?>
       <?php endif; ?>
       </ul>
       ```
   

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/making-a-function/#post-1087398)
 * Don’t know why the function is necessary, but try this:
 *     ```
       <?php
       function get_project_active_pages() {
       $args = array(
         'post_type' => 'page',
         'meta_key' => 'project_active',
         'meta_value' => 'yes'
       ); 
   
       return get_posts($args);
       }
       ?>
   
       <?php
       $pageposts=get_project_active_pages();
       if ($pageposts): ?>
       <ul>
       <?php foreach ($pageposts as $post):
       setup_postdata($post);
       ?>
       <li><a href="the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
       <?php endforeach; ?>
       <?php endif; ?>
       </ul>
       ```
   
 *  Thread Starter [xhan](https://wordpress.org/support/users/xhan/)
 * (@xhan)
 * [17 years ago](https://wordpress.org/support/topic/making-a-function/#post-1087543)
 * thank you so much michael!
 * I need to be able to display all active projects at once but they wont always
   be the most recent pages. that was the only way i could think of!

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

The topic ‘making a function’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * 2 replies
 * 2 participants
 * Last reply from: [xhan](https://wordpress.org/support/users/xhan/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/making-a-function/#post-1087543)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
