Page 1 of 1

How to exclude certain entries?

Posted: Wed Jul 06, 2011 8:00 am
by nyo
Hi,

I have a table for pages and I am listing them in the navbar. I want to exclude certain pages, how can I do that?

Here is the function I'm using to get the pages.
function get_pages() {
	$pages = array();
	$query = mysql_query("SELECT 
							`posts`.`id` AS `post_id`,
							`post_title`,
							`post_name`,
							`post_body`
							FROM `posts`
							WHERE `post_type` = 'page'
							ORDER BY `post_id` ASC");
	while ( $row = mysql_fetch_assoc($query)) {
		$pages[] = $row;
	}
	return $pages;
}
and here is the code that I use to list them:
<?php
			foreach (get_pages() as $page) {
			?>
			<li><a href="page.php?p=<?php echo $page['post_name']; ?>"><?php echo $page['post_title']; ?></a></li>
			<?php
			}
		?>

Re: How to exclude certain entries?

Posted: Wed Jul 06, 2011 8:48 am
by conradk
nailyener wrote:and here is the code that I use to list them:
<?php
			foreach (get_pages() as $page) {
			?>
			<li><a href="page.php?p=<?php echo $page['post_name']; ?>"><?php echo $page['post_title']; ?></a></li>
			<?php
			}
		?>
New code that might work:
<?php
$unwanted = array('contact', 'another'); // list the unwanted page 'post_name' s here. In this case, the 'contact' page and the 'another' page would not show up
foreach (get_pages() as $page) {
   if(!in_array($page['post_name'], $unwanted)) {
      echo '<li><a href="page.php?p=' .  $page['post_name'] . '">' . $page['post_title'] . '</a></li>';
   }
}
?>
Tell me if there's any problem.

You could also choose to show only the pages listed in the $unwanted array, by simply removing the ! from the 4th line of my code.

Regards,
CK

Re: How to exclude certain entries?

Posted: Wed Jul 06, 2011 12:14 pm
by jacek
The $unwanted array should be defined outside of the loop ;)

You could do something similar with just SQL too
SELECT
    `posts`.`id` AS `post_id`,
    `post_title`,
    `post_name`,
    `post_body`
FROM `posts`
WHERE `post_type` = 'page'
AND `post_name` NOT IN ('list', 'of', 'excluded', 'pages')
ORDER BY `post_id` ASC

Re: How to exclude certain entries?

Posted: Wed Jul 06, 2011 1:20 pm
by conradk
jacek wrote:The $unwanted array should be defined outside of the loop ;)

Of course..; don't know why I declared it inside of the loop actually. My mistake. Fixed.