Có phải bạn muốn thiết kế website tiết kiệm chi phí và thời gian mà còn chuẩn seo ?

12/11/13

Hiển thị bài POST với điều kiện của meta_box hay custom field

Bài toán đặt ra: Viết chức năng của bài post gồm có: nút check, những bài viết nào mà được checked thì mới hiện thi ra trang chủ. Để viết được nút check box thì bạn dùng custom field, hay meta_box (bài này mình sẽ hướng dẫn sau)

Để hiện thị những bài viết nào mà được checked trong admin thì bạn hiểu đơn giản sau: ví dụ mình có 1 check box như sau

<input type="checkbox" name="is_home" value="1"/>

Thì khi lưu bài viết, giá trị có thuộc tính name="is_home", và thuộc tính value="1" được lưu trong bảng post_meta

id         post_id   meta_key   meta_value
xxx       xxx         is_home     1

Để lấy bài viết chỉ khi checked (trong admin) ra ngoài thì bạn hiểu như sau: Lấy những bài viết có is_home=1

Tham khảo đoạn code sau:

query_posts(array(
        'post_type' => 'post',
        'orderby' => $order,
        'cat' => $category,
        'showposts' => 3,
        'meta_query' => array(
            array(
                'key' => 'is_home',
                'value' => '1',
            )
        )

    ));

==> Hiển thị bài viết có danh mục $category (biến truyền vào), kiểu bài viết là "post", sắp xếp thứ tự theo biến $order (biến truyền vào), lấy 3 bài viết thôi và lấy những bài viết nào có is_home=1

Tham khảo những cách sau
Example 1

<?php 
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_key' => 'location',
 'meta_value' => 'Melbourne'
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 2

<?php 
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_query' => array(
  'relation' => 'AND',
  array(
   'key' => 'location',
   'value' => 'Melbourne',
   'compare' => '='
  ),
  array(
   'key' => 'attendees',
   'value' => 100,
   'type' => 'NUMERIC',
   'compare' => '>'
  )
 )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 3

<?php 
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_query' => array(
  'relation' => 'OR',
  array(
   'key' => 'location',
   'value' => '%Melbourne%',
   'compare' => 'LIKE'
  ),
  array(
   'key' => 'location',
   'value' => '%Sydney%',
   'compare' => 'LIKE'
  )
 )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 4

<?php 
 
// args
$args = array(
    'numberposts' => -1,
    'post_type' => 'event',
    'meta_query' => array(
        array(
            'key' => 'images',
            'value' => 0,
            'type' => 'NUMERIC',
            'compare' => '>'
        )
    )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php // load 'images' repeater field data. Please see repeater field for code examples ?>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 5

?php 
 
// custom filter to replace '=' with 'LIKE'
function my_posts_where( $where )
{
 $where = str_replace("meta_key = 'images_%_type'", "meta_key LIKE 'images_%_type'", $where);
 
 return $where;
}
 
add_filter('posts_where', 'my_posts_where');
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_query' => array(
  array(
   'key' => 'images_%_type',
   'value' => 'type_1',
  )
 )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>



REF: http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

















5 comments:

  1. bai dang hay lam .. rat bo ich
    -------------------------------------------------------------------------------------------------------
    Dịch vụ seo website chuyên nghiệp tại tpHCM
    Web: http://dichvuseowebz.blogspot.com/
    Click vào Keywords: dịch vụ seo website chuyên nghiệp tại tpHCM
    Click vào Keywords: dich vu seo website chuyen nghiep tai tpHCM

    ReplyDelete
  2. If you don't mind, then continue this excellent work and expect more from your great blog posts

    data science course

    ReplyDelete
  3. Somebody Sometimes with visits your blog normally and prescribed it as far as I can tell to peruse too.
    iot training in delhi

    ReplyDelete
  4. I see some amazingly important and kept up to length of your strength searching for in your on the site
    hrdf contribution

    ReplyDelete
  5. I curious more interest in some of them hope you will give more information on this topics in your next articles.
    full stack web development course in malaysia

    ReplyDelete