0

Hello I have created a custom php file that I use to show specific posts by value inside the a custom field. So for example I have 100 posts, let's say they all have custom field MyTitle and 10 of them have value "New".

$querystr = "
            SELECT $wpdb->posts.* 
            FROM $wpdb->posts, $wpdb->postmeta
            WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
            AND $wpdb->postmeta.meta_key = 'Mytitle' 
            AND $wpdb->postmeta.meta_value LIKE '%".$mytitle."%' 
            AND $wpdb->posts.post_status = 'publish' 
            AND $wpdb->posts.post_type = 'post'
            AND $wpdb->posts.post_date < NOW()
            ORDER BY $wpdb->posts.post_date DESC
         ";

         $pageposts = $wpdb->get_results($querystr, OBJECT);

So I use the code to display those posts, however I wanted to add a pagination.I've done some search here and on the internet, but all the pagination is rather different talking about getting "paged" from the main query.. I don't really understand that part. So can you show me examples which I can use with the code that I've shown here?

0

I will start with this, never pass raw variables to a sql query! unless you have 100% control over the variable content, and even then I would still not pass it raw.
Always use prepare().

Now going by your need you will need to do something like this

<?php
$paged = (get_query_var( 'paged')) ? get_query_var('paged') : 1;

$posts = new WP_Query([
    'post_type'      => 'post',
    'posts_per_page' => 1,
    'post_status'    => 'publish',
    'meta_key'       => 'Mytitle',
    'meta_value'     => $mytitle,
    'meta_compare'   => 'LIKE',
    'paged'          => $paged
]);
?>

<ul>

<?php if ($posts->have_posts()) : while ($posts->have_posts()) : $posts->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile; endif;

wp_reset_postdata();
?>

</ul>

<div class="pagination">
    <?php 
        echo paginate_links([
            'base'         => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
            'total'        => $posts->max_num_pages,
            'current'      => max( 1, get_query_var('paged')),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf('<i></i> %1$s', __('Newer Posts', 'text-domain')),
            'next_text'    => sprintf('%1$s <i></i>', __('Older Posts', 'text-domain')),
            'add_args'     => false,
            'add_fragment' => '',
        ]);
    ?>
</div>

This is a very raw example so you will need to do changes based on your needs.

Some resources to get a better idea on how everything works.

  1. WP_Query class
  2. Looping WP_Query
  3. Pagination with WP_Query WPSE
  4. Wordpress pagination codex
5
  • I actually check for specific contents - only, of the variable value from the URL before passing it to the sql query. However I will try your example. Oct 12 at 8:38
  • The code is working, showing the results and links, however the links are not working.I believe this is because I have used a custom rewrite rule in my functions.php file so that theurl works something like : website/mycustomphppage/new/ so the link now is website/mycustomphppage/new/page/2/ add_rewrite_rule( 'mycustomphppage/([^/]*)/?$', 'index.php?pagename=mycustomphppage&mytitle=$matches[1]', 'top'); Do you have any advice how to add the pagination to this rule? Oct 12 at 8:51
  • Unfortunately no, Im not that good with rewrite rules to tell you what causes this issue, maybe @MrWhite could help out, he has a loads of experience with rewrites so, if he sees this post, maybe he could shed some light on this issue =]. Oct 12 at 8:58
  • @WordpressGuest I have found this article that talks about pagination with rewrite rules, maybe that could give you more information. Oct 12 at 9:22
  • I have managed to make it work, first it did not work as I was trying to look for page query var, when the page query var is named paged , so this works(added another rule): add_rewrite_rule( 'mycustomphppage/(.+?)/page/(.+?)/?$', 'index.php?pagename=mycustomphppage&mytitle=$matches[1]&paged=$matches[2]', 'top'); Oct 12 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.