当我想像我的帖子的WP_Query列表中那样使用自定义分页时,如何设置get_results()的posts_per_page?

时间:2019-02-18 15:35:32

标签: php wordpress pagination

我通过搜索查询搜索了一些帖子,我需要设置posts_per_page,然后像在WP_Query()的循环中那样设置分页

问题是,我不知道要通过WP_Query()数组获取搜索数据。

我通过get_results()搜索到的帖子:

    <?php get_header();?>

    <?php

    if($_GET['search_text'])
    {
        $text = $_GET['search_text'];
    }
    ?>

    <?php $query = null;?>
    <div class="container">

        <h1 class="mb-2" style="font-size: 25px;">Searching for : <?php echo $text;?></h1>
        <?php
        global $wpdb;
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $myposts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts 
WHERE post_type = 'advertisements' 
AND post_title != 'Automatický koncept' 
and post_title LIKE '%s'", '%'. $wpdb->esc_like( $text ) .'%')  );

        ?>


        <?php foreach ( $myposts as $mypost )
        {   $post = get_post( $mypost );?>

        <?php } ?>
    </div>

    <?php get_footer();?>

我在WP_Query()中的自定义分页代码,感谢上帝,它有效:

<?php get_header()?>
<div class="container">
    <h1 class="ml-3 mb-3">Custom Posts of Logged User</h1>
     <?php
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     $args = array('post_type' => 'advertisements',
            'author' => get_current_user_id(),
            'posts_per_page' => 2,
            'paged' => $paged
     );

        $wp_query = new WP_Query( $args );
     if($wp_query->have_posts()) :
            while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

            <?php endwhile;?>
         <?php echo bootstrap_pagination_subpage($wp_query);?>
         <?php   wp_reset_postdata(); ?>

    <? else :

         ?>
     <h2>You do not have Custom Advertisements yet</h2>

    <?php endif;?>

</div>
<?php get_footer()?>

请问有可能吗?

0 个答案:

没有答案