• I have a Page template with the following query to list all posts ordered by the “Author” custom field (i.e., author of the document posted, not the author of the post).

    <?php
    $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'Author' AND
    wposts.post_status = 'publish' ORDER BY wpostmeta.meta_value ASC", OBJECT);
    if ($pageposts):
    foreach ($pageposts as $post):
    setup_postdata($post); ?>
    
    ... etc.

    How can I paginate this instead of listing all 360+ posts at once?

Viewing 6 replies - 16 through 21 (of 21 total)
  • <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $postsperpage = 30;
    $offset = ($paged-1) * $postsperpage
    $pageposts = $wpdb->get_results("SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE ... blah ...
    LIMIT $offset, $posts_per_page ", OBJECT);
    ...
    foreach ($posts as $post) :
    setup_postdata($post); ?>
        (loop stuff)
    <?php endforeach; ?>

    Yeah, this works very well!

    I have been trying to get the code above to display the navigation links but I cant get it to work.

    I have done exactly as described in the code listed by ericr23 and my “navigation stuff” contains the code

    previous_post('&laquo; %', ''); next_post('% &raquo;', '');

    But the links are not showing. I have wordpress 2.5.1, earlier versions exhibited the same issue, custom query works but navigation doesnt appear.

    Would appreciate some input from people, particularly like to see the navigation code being used by ericr23.

    Hi,

    The initial question was good. I had the same problem – have page with custom query
    $pageposts = $wpdb->get_results(“select …”, OBJECT);
    if ($pageposts):
    foreach ($pageposts as $post):
    setup_postdata($post); ?>)

    with many post as a result.

    The problem is that navigation functions next_posts_link and previous_posts_link don’t work here. Of course, it works if one writes his own navigation functions, but I prefer to use WP navigation way. So, the solution is to ‘put’ this query in the loop ant to use wordpress functionality.

    Just change the search form(searchform.php) and add the following line <input type=”hidden” name=”key” value=”<what you want>” /> – just before <input type=”submit” id=”searchsubmit” value=”Search” />
    Let say you have
    <input type=”hidden” name=”key” value=”pretty” />
    Thus the url by searching something(f.i. word ‘dog’) would be
    https://&#8230;../?s=dog&key=pretty

    Then get the plugin Search Custom Fields and modify it a bit. The functions in the plugin get
    the search word (dog) and the value of the key(pretty).
    Add one if() to szub_search_custom_join and szub_search_custom_where functions – if the value of the key is ‘pretty’, change the functions szub_search_custom_join and szub_search_custom_where in such way, like normal search is performed – only the word ‘dog’ will be searched without any additional JOIN and WHERE
    f.i.
    function szub_search_custom_where($where) {


    if ($_GET[‘key’] == ‘pretty’) {
    $where = ” (1=1) AND $status “;
    }
    }
    return $where;

    But if key has other value – f.i. ‘myname’, then set variable $join in szub_search_custom_join and $where in szub_search_custom_where so, like your custom query will be executed (you can use also search word here – dog in this example).

    So, if you use https://&#8230;../?s=dog&key=myname – then the output will be your custom query. And this will come from the loop – so navigation is ok and so on.

    Hope this help.

    Regards.

    Hi Georgi
    Thanks for the reply.
    Interesting approach but not convenient for my needs.

    I have just managed to find a work around using the pagebar navigation (which I was already using) https://www.elektroelch.de/hacks/wp/pagebar/

    I amended this plugin slightly and now with custom query page, I can navigate by using the above plugin as per all my non custom pages.

    So problem sorted for me ??

    great post, thanks for contributing everyone!

    Thread Starter ericr23

    (@ericr23)

    Sorry, I haven’t checked in for a while.

    The bottom-of-page navigation I used is posted above, but one might miss the later note that $numpages needs to be renamed. So here is the final:

    <?php
    $precpage=$paged-1;
    $nextpage=$paged+1;
    if ($paged>1) {
    if ($precpage==1)
    	{ echo ('<a href="../../">« Home</a>'); }
    	else
    	{ echo ('<a href="../' . $precpage . '/">« Preceding Page</a> • <a href="../../">Home</a>'); }
    }
    if ($nextpage<=$numofpages) {
    	if ($paged==1)
    		{ echo ('<a href="./page/'); }
    		else
    		{ echo (' • <a href="../'); }
    	echo ($nextpage . '/">Next Page »</a>');
    	}
    ?>

    And here is a different navigation scheme for the top of the page:

    <?php
    echo ('<b>[</b> &nbsp;' . $numposts . ' documents &nbsp;•&nbsp; <i>pages:&nbsp;</i> ');
    $i=1; while ($i<=$numofpages) {
    if ($i==$paged) {
    echo (' <b>' . $i . '</b>'); }
    else {
    	if ($i==1)
    	{ echo (' <a href="../../">' . $i . '</a>'); }
    	else
    	{ if ($paged==1)
    	{ echo (' <a href="./page/' . $i . '/">' . $i . '</a>'); }
    	else
    	{ echo (' <a href="../' . $i . '/">' . $i . '</a>'); } } }
    $i++;
    }
    echo ('&nbsp; <b>]</b>'); ?>

    Again, see them at work at https://www.wind-watch.org/documents/authors/

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘how to page custom database query (to sort by custom field)?’ is closed to new replies.