WordPress Paginare $wpdb->get_results

24 mai 2012, 21:46:23
Vizualizări: 24.7K
Voturi: 8

Datorită unei configurații complexe multisite, am o interogare care combină articolele din două bloguri și aș dori să paginez rezultatele. Sunt recunoscător pentru orice ajutor. Mai jos este interogarea mea.

            $latestposts = $wpdb->get_results(

            "
            (SELECT * FROM net_5_posts
            INNER JOIN net_5_term_relationships ON net_5_posts.ID=net_5_term_relationships.object_id  
            WHERE post_type = 'post' 
            AND post_status = 'publish' 
            AND term_taxonomy_id = '151' 
            )

            UNION ALL

            (SELECT * FROM net_7_posts
            INNER JOIN net_7_term_relationships ON net_7_posts.ID=net_7_term_relationships.object_id  
            WHERE post_type = 'post' 
            AND post_status = 'publish' 
            AND term_taxonomy_id = '20' 
            )

            ORDER BY post_date
            DESC LIMIT 5",'ARRAY_A');

            foreach ($latestposts as $latestpost) {

            $da_id = $latestpost['ID'];
            $da_title = $latestpost['post_title'];
            $da_content = strip_tags($latestpost['post_content']);
            $da_content = limit_words($da_content,55); // Limitează numărul de cuvinte la 55
            $da_link = $latestpost['guid'];
            $da_date = $latestpost['post_date'];
            $da_date = date('F j, Y', strtotime($da_date));

            echo '
            <div class="ldapost">
            <h2 class="lheader"><a href="'.$da_link.'">'.$da_title.'</a></h2>
            <span class="ldate">'.$da_date.'</span>
            <span class="lcontent">'.$da_content.'…</span><br>
            <a class="button btnright" href="'.$da_link.'">Continuă Lectura</a>
            </div>
            ';

            }
0
Toate răspunsurile la întrebare 2
6
14

Actualizare

Am testat acest lucru și funcționează pe site-ul meu. Câteva observații:

  • Înlocuiește $query-ul meu cu al tău
  • global $wpdb (conform comentariului tău despre variabilele globale) deoarece este în afara domeniului!
  • get_results() returnează un obiect dacă nu i se specifică altfel (al doilea parametru este tipul de return)
  • Am plasat acest cod într-un plugin, dar îl poți extrage și pune în tema ta sau pur și simplu în functions.php.

Iată funcția:

function test_function() {

    global $wpdb;

    $query = "
        (SELECT * FROM wp_18_posts
        INNER JOIN wp_18_term_relationships ON wp_18_posts.ID=wp_18_term_relationships.object_id  
        WHERE post_type = 'post' 
        AND post_status = 'publish' 
        AND term_taxonomy_id = '2')

        UNION ALL

        (SELECT * FROM wp_17_posts
        INNER JOIN wp_17_term_relationships ON wp_17_posts.ID=wp_17_term_relationships.object_id  
        WHERE post_type = 'post' 
        AND post_status = 'publish' 
        AND term_taxonomy_id = '2')";

    $total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
    $total = $wpdb->get_var( $total_query );
    $items_per_page = 1;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
    $offset = ( $page * $items_per_page ) - $items_per_page;
    $latestposts = $wpdb->get_results( $query . " ORDER BY post_date LIMIT ${offset}, ${items_per_page}" );

    foreach ($latestposts as $latestpost) {
        $da_id = $latestpost->ID;
        $da_title = $latestpost->post_title;
        $da_content = strip_tags($latestpost->post_content);
        $da_content = wp_trim_words($da_content, 55);
        $da_link = $latestpost->guid;
        $da_date = $latestpost->post_date;
        $da_date = date('F j, Y', strtotime($da_date));

        echo '
        <div class="ldapost">
        <h2 class="lheader"><a href="'.$da_link.'">'.$da_title.'</a></h2>
        <span class="ldate">'.$da_date.'</span>
        <span class="lcontent">'.$da_content.'…</span><br>
        <a class="button btnright" href="'.$da_link.'">Continuă Citirea</a>
        </div>
        ';
    }

    echo paginate_links( array(
        'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'prev_text' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        'total' => ceil($total / $items_per_page),
        'current' => $page
    ));
}

Postare Originală

Funcția paginate_links este independentă de interogarea ta. Având în vedere câțiva parametri, cum ar fi numărul total de articole și pagina curentă, poate oferi paginarea pe care o cauți. Deci trebuie să calculezi:

  1. Numărul total de articole
  2. Numărul paginii curente (bazat pe 1)
  3. Offset-ul pentru instrucțiunea LIMIT din MySQL.

Mă gândeam la ceva de genul acesta (netestat, scuze!):

$query = "
    (SELECT * FROM net_5_posts
    INNER JOIN net_5_term_relationships ON net_5_posts.ID=net_5_term_relationships.object_id  
    WHERE post_type = 'post' 
    AND post_status = 'publish' 
    AND term_taxonomy_id = '151' 
    )

    UNION ALL

    (SELECT * FROM net_7_posts
    INNER JOIN net_7_term_relationships ON net_7_posts.ID=net_7_term_relationships.object_id  
    WHERE post_type = 'post' 
    AND post_status = 'publish' 
    AND term_taxonomy_id = '20' 
    )";

$total = $wpdb->get_var( "SELECT COUNT(1) FROM (${query}) AS combined_table" );
$items_per_page = 5;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$latestposts = $wpdb->get_results( $query . " ORDER BY post_date LIMIT ${offset}, ${items_per_page}" );

foreach ($latestposts as $latestpost) {
    // Codul tău aici ...
}

echo paginate_links( array(
    'base' => add_query_arg( 'cpage', '%#%' ),
    'format' => '',
    'prev_text' => __('&laquo;'),
    'next_text' => __('&raquo;'),
    'total' => ceil($total / $items_per_page),
    'current' => $page
));

Referințe:

24 mai 2012 21:52:09
Comentarii

mersi, hmmm nu prea am reușit să fac asta să funcționeze cu interogarea mea, probabil îmi scapă ceva.

uknowit2 uknowit2
24 mai 2012 22:23:45

Am actualizat răspunsul meu să fie mai clar, cu niște pseudocod. Sper să meargă acum! Dacă nu, te rog comentează! :)

getWeberForStackExchange getWeberForStackExchange
25 mai 2012 00:27:59

Hei Weberwithoneb, mersi că ai rămas cu mine la asta. Am încercat interogarea actualizată dar afișează gol, fără erori, doar fără rezultate. Pot confirma că interogarea din întrebarea mea funcționează. Trebuie să adaug niște globale?

uknowit2 uknowit2
25 mai 2012 11:25:16

Ok, am actualizat răspunsul meu cu cod testat. Vă rugăm să comentați dacă aveți întrebări!

getWeberForStackExchange getWeberForStackExchange
25 mai 2012 19:57:21

Wow!! Ești un GENIU și vreau să-ți spun un MARE MULȚUMESC!! Funcționează perfect!! Mulțumesc din nou.

uknowit2 uknowit2
25 mai 2012 23:37:10

Mă bucur să aud asta!!

getWeberForStackExchange getWeberForStackExchange
26 mai 2012 01:45:39
Arată celelalte 1 comentarii
0
// Acest cod a funcționat excelent pentru mine, mulțumesc mult! L-am adaptat pentru ceea ce aveam nevoie. Direct în fișierul template, Perfect!
global $wpdb;
// INTEROGARE AICI PENTRU A NUMĂRA TOTALUL DE ÎNREGISTRĂRI PENTRU PAGINARE $total = $wpdb->get_var("SELECT COUNT(*)
$post_per_page = 10;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $post_per_page ) - $post_per_page;

// INTEROGARE AICI PENTRU A OBȚINE REZULTATELE $results = $wpdb->get_results

// PHP BUCLA FOR EACH AICI PENTRU AFIȘAREA REZULTATELOR
// FINALIZARE BUCLA FOR EACH

// PAGINARE AICI ÎN STILURI BOOTSTRAP FRUMOASE
<?php 
echo '<div class="pagination">';
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('&laquo;'),
'next_text' => __('&raquo;'),
'total' => ceil($total / $post_per_page),
'current' => $page,
'type' => 'list'
));
echo '</div>';
?>
8 aug. 2014 23:42:17