Support » Fixing WordPress » Paging while using a custom select query

  • Resolved michuk


    I need to use a custom search query on my index.php page (I just display the posts that have a special value in a special custom field). I used this approach: to implement it.

    I just have a problem with paging. It displays all the posts from the query. How do I add default wordpress paging to the query (so that it shows only 5 – or whatever else is defined in WP – posts and “prev”, “next” buttons)?

Viewing 10 replies - 1 through 10 (of 10 total)
  • I could work with you to produce a proper SQL query for this, calling up the ‘paged’ and ‘posts_per_page’ values through the get_query_var() WordPress function to evaluate a proper LIMIT clause, and so on.

    But you know what? I believe a plugin would be much cooler…

    Custom Home
    Download plugin | View source

    Before you activate the plugin on your blog, it has two user-configurable variables you should know about:

    $key = The custom field key the plugin looks for. Default key name is ‘home’.

    $value = The custom field value the plugin looks for. This is optional, and if left empty the plugin will match on the key only. Default value is ”.

    You can change these variables either through the Plugin Editor in WordPress, or in a text editor offline.

    Two points about the plugin: You’ll have to revert to The Loop in your index.php to use it. And yes, it plays nice with next/prev pagination.

    Kafkaesqui – thanks a lot. I wanted to implement it as a plugin, too, but did not know how to dig into this… your code is a good start.

    I need to customize the query (is it intensional that the WHERE clause relaces the original clause instead of appending it? — it displays unpublished posts, pages and everything by default), but this is still much easier than building own templates with custom queries for each one.

    One more thing though: do you know how to specify that this plugin should be used only on certain pages, in my case, a “/something/something_else/” page (which uses the a template something.php)?

    Right now it just does a check for home but probably there is a way to check for other pages (also the non-standard ones) too.

    is it intensional that the WHERE clause relaces the original clause instead of appending it?

    That was *un*intentional. I’ve updated the plugin (R1.0.1) to provide a fix.

    RE: One more thing

    I’d look over the conditionals available in WordPress and see if any work for you:

    If by ‘certain pages’ you mean Pages, I’m not sure my work here will help as a Page can’t loop multiple posts through The Loop *unless* you’re overriding it with something like query_posts(). They also don’t paginate posts (well, not without a lot of work!).

    The parenthetical “non-standard ones” also leaves me wondering if the plugin will work here, considering my assumptions in its creation are based on your original mention of index.php.

    Thanks a lot. I used the approach from your plugin and build my own for my particular purpose. I achieved what I wanted so I’m marking the thread as resolved. Thanks again.

    Thanks Kafkaesqui; I was led here in my quest to find a simple way to display posts in a particular category by the post_modified date… your code provided more help than I have found in all my other travels.

    Hey, thanks very much! I was really needing that!!

    One prob! how to set to show only 5 posts per page??

    I tried with: query_posts(‘posts_per_page=5’); before the loop, It shows only 5 and create the arrow, but It gives me error when I tried to access the link to next/previous page… =(

    HELP! 😀

    Hello Kafkaesqui,

    Could you please help me a little bit? I really liked your plugin and it helped me a lot, I just want to know… if you can help me, use it in another pages besides “home”. I have a category templates, and I wanted to use it in this template too, could you help me?


    Hey Kafkaesqui,

    Could you help in the question above??

    I need to can use this plugin with more than one key/value…. Like, in the “home page” use the key/value = test/test and in the page “top news” use like test2/test2.

    Thank you very much.

    Hello everybody, I’m really needing to be possible use this plugin in other than the page… I mean, more than 2 places, with keys/values different. I tried to make a “if is_page()” inside the plugin code to give different values to the variable of the key/values, but it didn’t work =/

    Really appreciate your help.

    Kafkaesqui, you’ve made my day! Thank you for your code, I adapted it to suit my needs.

    Make sure you insert any additional ‘if’ statements before the ‘posts_where’ function returns. You should be able to use as many as you like for any number of situations. Just make sure you follow the format of the examples listed on the Conditional Tags page that Kafkaesqui linked to.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Paging while using a custom select query’ is closed to new replies.