WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Paging while using a custom select query (11 posts)

  1. michuk
    Member
    Posted 7 years ago #

    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: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query 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)?

  2. Kafkaesqui

    Posted 7 years ago #

    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.

  3. michuk
    Member
    Posted 7 years ago #

    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.

  4. Kafkaesqui

    Posted 7 years ago #

    "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:

    http://codex.wordpress.org/Conditional_Tags

    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.

  5. michuk
    Member
    Posted 7 years ago #

    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.

  6. Xander
    Member
    Posted 7 years ago #

    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.

  7. usuario
    Member
    Posted 7 years ago #

    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! :D

  8. usuario
    Member
    Posted 7 years ago #

    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?

    thanks.

  9. usuario
    Member
    Posted 7 years ago #

    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.

  10. usuario
    Member
    Posted 7 years ago #

    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.

  11. joel01
    Member
    Posted 6 years ago #

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

    @usuario:
    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.

Topic Closed

This topic has been closed to new replies.

About this Topic