0

I have been working on a shortcode to query pages by category and display custom fields. I have everything working great except; if my 'custom_field' variable is "numeric", it returns the correct number of 'posts_per_page', if my 'custom_field' variable is "text", it doesn't return anything unless the 'posts_per_page' is entered in multiples of 10. For example, if I want to show '5' posts, I have to enter 'posts_per_page="50".

I am completely baffled.

On a different note, I have been reading that I should not use "extract(shortcode_atts(" but I am not able to pass my variables without doing it this way. How should this be written?

As always, I really appreciate your guidance.

// Begin Shortcode

add_shortcode("custom_query_test_100", "custom_query_test_100");

function custom_query_test_100($atts, $content = null) {

    // Attributes
    extract( shortcode_atts(
        array(
            'pagination' => 'true',
            'query' => '',
            'category' => '',
            'custom_field' => '',
            'number_posts' => '',
            'order_by'   => '',
            'order'     => '',      
        ), $atts ));


    // WP_Query arguments
    $args = array(
        'paged'                  => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
        'post_type'              => array( 'page', ' post' ),
        'post_status'            => array( 'publish' ),
        'cat'                    => $category,
        'meta_key'               => $custom_field,
        'posts_per_page'         => '50',
        'order'                  => $order,
        'orderby'                => $order_by,
        );

    // The Query
    global $wp_query,$paged,$post;
    $query = new WP_Query( $args );

    ob_start(); 

    if ($query->have_posts()):
    while ($query->have_posts()) : $query->the_post();

    if ( strlen(get_post_meta($post->ID, $custom_field, true)) > 0 ) :
    ?>

    <div style="margin:10px 0 30px;"></div>
        <a href="<?php the_permalink() ?>">
        <h2 style="margin:0 0 5px 0; font-size:24px;"><?php echo the_title(); ?></h2>       
        <div class="one-third-width-responsive">        
        <?php echo the_post_thumbnail( 'large' );  ?>       
        </div>
        </a>

        <div class="two-third-width-responsive">        
        <div style="font-size:15px;"><?php echo get_post_meta($post->ID, $custom_field, true); ?></div>
        </div>

        <?php endif; ?>

    <div class="clearfix"></div>

    <?php 
    endwhile;     
    endif;
    ?>

    <div style="margin-top:35px;"></div>

    <?php if ($query->max_num_pages > 1) : // custom pagination  ?>
        <?php
        $orig_query = $wp_query; // fix for pagination to work
        $wp_query = $query;
        ?>

        <div style="margin:20px 0; background-color:#e3f0f7; width:100%; padding:30px;">
            <div style="float:left; line-height:0;"><?php echo get_previous_posts_link('« Previous'); ?></div>
            <div style="float:right; line-height:0;"><?php echo get_next_posts_link('Next »', $query->max_num_pages ); ?></div>
        </div>

       <?php
        $wp_query = $orig_query; // fix for pagination to work

        endif;      

    wp_reset_postdata();    

    $output = ob_get_clean();
    //print $output; // debug
    return $output; 

}

// End

1 Answer 1

-1

You follow this link:https://codex.wordpress.org/Function_Reference/paginate_links i hope your problem solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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