Md Mehedi Hasan

WP_Query | custom query

post__in

<?php
/*
* Template Name: Custom Query WPQuery
*/
 
?>
 
<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">
 
    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = new WP_Query(array(
        'post__in' => $post_ids,
        'order' => 'post__in',
        'posts_per_page' => $post_per_page,
        'paged' => $paged
 
    ));
 
    while ($_P->have_posts()) {
        $_P->the_post();
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_query();

    ?>
 
 
    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    'total'=> $_P->max_num_pages,
                    'current'=> $paged,
                    'prev_text' => __('New Post', 'alpha'),
                    'next_text' => __('Old Post', 'alpha'),
                ));
                ?>
            </div>
        </div>
    </div>
 
 
</div>
 
 
<?php get_footer(); ?>

category_name

<?php
/*
* Template Name: Custom Query WPQuery
*/
 
?>
 
<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">
 
    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = new WP_Query(array(
        'category_name' => 'uncategorized',
        'posts_per_page' => $post_per_page,
        'paged' => $paged
 
    ));
 
    while ($_P->have_posts()) {
        $_P->the_post();
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_query();

    ?>
 
 
    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    'total'=> $_P->max_num_pages,
                    'current'=> $paged,
                    'prev_text' => __('New Post', 'alpha'),
                    'next_text' => __('Old Post', 'alpha'),
                ));
                ?>
            </div>
        </div>
    </div>
 
 
</div>
 
 
<?php get_footer(); ?>

wp query relationship and joining

<?php
/*
* Template Name: Custom Query WPQuery
*/
 
?>
 
<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">
 
    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = new WP_Query(array(
        'posts_per_page' => $post_per_page,
        'paged' => $paged,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array('uncategorized')
            ),
            array(
                'taxonomy' => 'post_tag',
                'field'    => 'slug',
                'terms'    => array('special')
            ),
        )
    ));
 
    while ($_P->have_posts()) {
        $_P->the_post();
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_query();

    ?>
 
 
    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    'total'=> $_P->max_num_pages,
                    'current'=> $paged,
                    'prev_text' => __('New Post', 'alpha'),
                    'next_text' => __('Old Post', 'alpha'),
                ));
                ?>
            </div>
        </div>
    </div>
 
 
</div>
 
 
<?php get_footer(); ?>

Post Status and Date query

<?php
/*
* Template Name: Custom Query WPQuery
*/
 
?>
 
<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">
 
    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = new WP_Query(array(
        'posts_per_page' => $post_per_page,
        'paged' => $paged,
        'post_status' => 'publish',
        'monthnum' => 7,
        'year'=> 2020

    ));
 
    while ($_P->have_posts()) {
        $_P->the_post();
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_query();

    ?>
 
 
    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    'total'=> $_P->max_num_pages,
                    'current'=> $paged,
                    'prev_text' => __('New Post', 'alpha'),
                    'next_text' => __('Old Post', 'alpha'),
                ));
                ?>
            </div>
        </div>
    </div>
 
 
</div>
 
 
<?php get_footer(); ?>

meta_key and meta_value

<?php
/*
* Template Name: Custom Query WPQuery
*/
 
?>
 
<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">
 
    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = new WP_Query(array(
        'posts_per_page' => $post_per_page,
        'paged' => $paged,
        'meta_key'=>'homepage',
        'meta_value'=>'1'
    ));
 
    while ($_P->have_posts()) {
        $_P->the_post();
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_query();

    ?>
 
 
    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    'total'=> $_P->max_num_pages,
                    'current'=> $paged,
                    'prev_text' => __('New Post', 'alpha'),
                    'next_text' => __('Old Post', 'alpha'),
                ));
                ?>
            </div>
        </div>
    </div>
 
 
</div>
 
 
<?php get_footer(); ?>

meta_query | relation | key | value | compare

<?php
/*
* Template Name: Custom Query WPQuery
*/
 
?>
 
<?php get_header(); ?>
<?php get_template_part("template-parts/common/hero"); ?>
<div class="posts text-center">
 
    <?php
    $paged = get_query_var("paged") ? get_query_var("paged") : 1;
    $post_per_page = 2;
    $post_ids =array(50, 22, 31, 37, 62, 57, 67);
    $_P = new WP_Query(array(
        'posts_per_page' => $post_per_page,
        'paged' => $paged,
        'meta_query'=>array(
            'ralation'=>'AND',
            array(
                'key'=>'featured',
                'value'=> '1',
                'compare'=> '='
            ),
            array(
                'key'=>'homepage',
                'value'=> '1',
                'compare'=> '='
            )
        )
    ));
 
    while ($_P->have_posts()) {
        $_P->the_post();
    ?>
        <h2> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php
    }
    wp_reset_query();

    ?>
 
 
    <div class="container post-pagination">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-8">
                <?php
                echo paginate_links(array(
                    'total'=> $_P->max_num_pages,
                    'current'=> $paged,
                    'prev_text' => __('New Post', 'alpha'),
                    'next_text' => __('Old Post', 'alpha'),
                ));
                ?>
            </div>
        </div>
    </div>
 
 
</div>
 
 
<?php get_footer(); ?>