• 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)
  • 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

    (@xhan)

    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.