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/5/13

Lấy POST theo post_meta, taxonomy trong wordpress

Ví dụ: trong website có chức năng hiển thị bài viết theo user.

User đó đăng nhập muốn xem những bài viết của mình (id_user được lưu trong meta_post).

Để hiển thị những bài post(bảng post) theo user (bảng metapost) thì chúng ta dùng đoạn code sau
$args = array(
    'post_type' => "buy_a_franchise",
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
    'orderby' => 'DESC',
    'meta_query' => array(
        array(
            'key' => 'id_user',
            'value' => $id_user,
        )
    )
);

Hiển thị tất cả bài post của theo ID user, ID user được lưu trong bảng post_meta.

Tiếp theo là đoạn code nâng cao hơn, hiển thị bài post theo post_meta, taxonomy

$args = array(
            'post_type' => $posttype,
            'post_status' => array($poststatus),
            'orderby' => 'rand',
            'posts_per_page' => 30,
            'meta_query' => array(
                array(
                    'key' => 'wpcf-paid',
                    'value' => array($paidvalue),
                    'compare' => 'IN',
                )
            ),
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => $taxtype,
                    'field' => 'slug',
                    'terms' => $geo
                ),
                array(
                    'taxonomy' => 'brands',
                    'field' => 'slug',
                    'terms' => $brands
                )
            )
        );
        $query->query_vars = $args;

0 comments:

Post a Comment