I wrote a simple plugin which creates a post type named cryptoevent and also creates a metabox that have some custom fields, I display this post type and fields with WP_Query, now I want to display them with a load more button, note this is supposed to be a plugin and I don't want user to have to do changes in his/her theme's function.php, here is my code, thank u for your help. and I'm a beginner, by the way, please help me in a simple language.

function cryptoevent_addform( $atts ) {

    $args = array( 'post_type' =>'cryptoevent','posts_per_page'=>8);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        echo '<div style="padding:0 15%;"><h2>';
        the_title(); ?>
        <p><?php echo get_post_meta( get_the_ID(), 'date_add', true ); ?></p>
        <p><?php echo get_post_meta( get_the_ID(), 'date_expiration', true ); ?></p>

        echo '</h2></div>';
add_shortcode( 'cryptoevent', 'cryptoevent_addform' );
  • Set the WP_DEBUG true in wp-config.php file and put the screenshot of the error. – Sagar Bahadur Tamang Feb 10 '19 at 6:33
  • thank for reply, this code works fine, i just want to display it with load more button – hessam hosseinipour Feb 10 '19 at 6:58

If you want it to work like a true "load more" button, then you are going to have to let the posts load in groups without the user changing pages. This means you have to use an AJAX method. This is much more complicated than the WordPress loop you have right now.

Overall it works like this, with your code split into 2 parts:

A. Code that displays results

  1. Request up to 10 results via AJAX (or another number)
  2. When they load, show them on the screen.
  3. If there are more results available, show a "load more" button
  4. If the user hits load more, go back to step 1

B. Code that fetches results

  1. Look at which results were requested (which page)
  2. Echo out the results

So your code that does A. above and your code that does B. above live in different places.

The A code is more or less what you have in the cryptoevent_addform function you wrote. It would be very different from what you have, but basically that is handling the front-end. The main difference is instead of getting the posts via WP_QUERY and PHP, it would be getting them via a JavaScript AJAX request, through the the WordPress AJAX system, to your B code.

The B code handles the back-end. It can also live in your plugin, but would need to be registered through the WordPress AJAX API.

Why do we have to do this? The normal flow of a page is (1) Backend (PHP/mySQL/etc), then (2) Frontend (HTML/JavaScript/etc.). But here we have to flip the order. You are letting a user (front-end) fetch posts from your database (back-end), without them having to reload a page. So to flip the order, we have to separate your code into the 2 parts and then use AJAX to start the page flow over.

You'll want to look at how to structure a WordPress AJAX call, which is more than we can cover in a Stack Exchange answer. See the documentation.

  • tnx for your clear reply, i know that i should use AJAX, and i've read the article that u introduced many times, but couldn't manage it. i will be really thankfull if i have the codes step by step. thank u so much – hessam hosseinipour Feb 10 '19 at 12:01
  • You are welcome. If you are happy with it please mark the answer as accepted so that others know it is already answered. – tmdesigned Feb 10 '19 at 12:04
  • I really appreciate you. but like i told you i'm looking for exact codes. – hessam hosseinipour Feb 10 '19 at 12:07
  • Ah, I see. Good luck. – tmdesigned Feb 10 '19 at 12:17

i used paginate instead, and use the code below

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>'cryptoevent', // your post type name
'posts_per_page' => 3, // post per page
'paged' => $paged,
if($data->have_posts()) :
while($data->have_posts())  : $data->the_post();
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
    $current_page = max(1, get_query_var('paged'));
    $big = 99999; // need an unlikely integer
    echo paginate_links(array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('« prev'),
        'next_text'    => __('next »'),
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();

it works fine if i set permalinks on plain, but my site permalinks are set on post name, please help me, thank you so much.

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.