go41

wordpress query exclude one post or page in the loop

von Joern am 22. Feb. 2011 | Keine Kommentare

A short code to exclude one or two posts or pages from the WordPress loop is easily done with ‚continue‘ This means if the loop comes over the post ID you state in this line, it will just continue with the next post.
In a normal loop add it just after the if (have_posts()) : while (have_posts()) : the_post(); stuff like this:

Code:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php  if( $post->ID == '5' ) continue; // post ID 5 will not show here ?>
the_title the_content ...
Actually I tried to exclude two pages from showing up in the SELECT FULLTEXT query of the plugin [url=http://bueltge.de/wp-landingsites-de-plugin/181/]Landing sites[/url]
I tried to get the code inside the query with the 'post__not_in' variable, but no idea how to get this array stuff in the given query.
So I added this 'continue' for two pages in the loop after the query of the plugin:
// Primary SQL query
      $sql = "SELECT ID, post_title, post_content,"
          . "MATCH (post_name, post_content) "
          . "AGAINST ('$terms') AS score "
          . "FROM $wpdb->posts WHERE "
          . "MATCH (post_name, post_content) "
          . "AGAINST ('$terms') "
          . "AND post_date <= '$now' "
          . "AND post_status = 'publish' " // modified - no 'static' anymore
          . "AND (post_type='post' OR post_type='page') " // query for pages too is set here in post_type
          . "ORDER BY score DESC LIMIT $limit";

      $results = $wpdb->get_results($sql);

      $output = '';
      if ($results) {
         foreach ($results as $result) {
         if( ($result->ID == '2') || ($result->ID == '4') ) continue; // exclude page ID 2 and page ID 4 here
            $title = stripslashes(apply_filters('the_title', $result->post_title));
            $permalink = get_permalink($result->ID);
            $post_content = strip_tags($result->post_content);
            $post_content = stripslashes($post_content);
//            $ctgry = get_the_category($result->ID); // this and the following line you could use to show the first of the categories the post is in
//            $output .= $before_title . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . $title . '">' . $title . '</a> in ' . $ctgry[0]->cat_name .  $after_title;
            $output .= $before_title . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . $title . '">' . $title . '</a>' . $after_title;
So here we use continue for two posts (or pages) to exclude them from the resulting posts in the query loop
if( ($result->ID == '2') || ($result->ID == '4') ) continue;

hope someone finds it useful

(von: Pit)

Sorry, no posts matched your criteria.

Autor:

Du findest mich auch auf Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

weitere forum Beiträge: