SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wordpress query_posts function

    I am attempting to just show a certain category on my index.php page, so I wanted to use the query_posts function to select just the certain posts from the category

    PHP Code:
    <?php if (have_posts()) : ?>
    <?php query_posts
    ('cat=3'); ?> //Category ID for index.php posts
    <?php while (have_posts()) : the_post(); ?> //The LOOP
    So, using that it just selects the correct posts, however, I get an error in my pagination function.

    PHP Code:
    function wp_pagenavi($before ''$after ''$prelabel ''$nxtlabel ''$pages_to_show 5$always_show false) {
        global 
    $request$posts_per_page$wpdb$paged;
        if(empty(
    $prelabel)) {
            
    $prelabel  '<strong>&laquo;</strong>';
        }
        if(empty(
    $nxtlabel)) {
            
    $nxtlabel '<strong>&raquo;</strong>';
        }
        
    $half_pages_to_show round($pages_to_show/2);
        if (!
    is_single()) {
            if(!
    is_category()) {
                
    preg_match('#FROM\s(.*)\sORDER BY#siU'$request$matches);        
            } else {
                
    preg_match('#FROM\s(.*)\sGROUP BY#siU'$request$matches);        
            }
            
    $fromwhere $matches[1];
            
    $numposts $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
            
    $max_page ceil($numposts /$posts_per_page);
            if(empty(
    $paged)) {
                
    $paged 1;
            }
            if(
    $max_page || $always_show) {
                echo 
    "$before <div class='Nav'><span>Pages ($max_page): </span>";
                if (
    $paged >= ($pages_to_show-1)) {
                    echo 
    '<a href="'.get_pagenum_link().'">&laquo; First</a> ... ';
                }
                
    previous_posts_link($prelabel);
                for(
    $i $paged $half_pages_to_show$i  <= $paged $half_pages_to_show$i++) {
                    if (
    $i >= && $i <= $max_page) {
                        if(
    $i == $paged) {
                            echo 
    "<strong class='on'>$i</strong>";
                        } else {
                            echo 
    ' <a href="'.get_pagenum_link($i).'">'.$i.'</a> ';
                        }
                    }
                }
                
    next_posts_link($nxtlabel$max_page);
                if ((
    $paged+$half_pages_to_show) < ($max_page)) {
                    echo 
    ' ... <a href="'.get_pagenum_link($max_page).'">Last &raquo;</a>';
                }
                echo 
    "<div class='NavEnd'></div></div> $after";
            }
        }

    This is the error message:

    Code:
    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
    SELECT COUNT(DISTINCT ID) FROM

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after the global $request line put

    var_dump($request);

    There has to be something wrong with that variable. Post the contents of that variable.

  3. #3
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    after the global $request line put

    var_dump($request);

    There has to be something wrong with that variable. Post the contents of that variable.
    Code:
    string(182) " SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND post_type = 'post' AND (post_status = 'publish' OR post_status = 'private') ORDER BY post_date DESC LIMIT 0, 10"

  4. #4
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try putting the query_posts before the if have_posts()

  5. #5
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    Try putting the query_posts before the if have_posts()
    same error

  6. #6
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,466
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Echo out your sql statement and run it in phpmyadmin. Usually seeing it printed on the screen, you can identify the problem right away, but if you can't, try running it.
    Sara

  7. #7
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree

    combine 'SELECT COUNT(DISTINCT ID) FROM' and $fromwhere to get your sql statement.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •