Lấy bài viết theo ACF Field nhưng order by theo ACF Field khác như thế nào?

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi seolagi, 13/3/24.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,030
    Đã được thích:
    81
    Điểm thành tích:
    48
    Như code ở dưới, em đang lấy tất cả bài viết trong wordpress bài viết theo ACF Field add_to_home khi được set cài đặt là 2:Yes, nhưng em lại muốn order by sắp xếp dữ liệu này lấy ra theo ACF Field khác có tên là stt_home thì làm như thế nào?
    Mã:
    $categoryId = 10;
    $post_id = 200;
    $args=array(
      'category__in' => $categoryId,
      'post__not_in' => array($post_id),
      'posts_per_page'=> 2, // Number of related posts that will be shown.
      'meta_key'      => 'add_to_home',
      'meta_value'    => '2:Yes'
    );
    
    Em lên mạng tìm hiểu thấy code ở dưới, nhưng áp dụng thì không được, vì em còn lấy dữ liệu theo 1 field truyền vào nữa hizzz, giúp em với.
    Mã:
    $posts = get_posts(array(
        'post_type'         => 'event',
        'posts_per_page'    => -1,
        'meta_key'          => 'start_date',
        'orderby'           => 'meta_value',
        'order'             => 'DESC'
    ));
    
     
    Cảm ơn đã xem bài:

    Lấy bài viết theo ACF Field nhưng order by theo ACF Field khác như thế nào?

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,909
    Đã được thích:
    1,201
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Em sửa lại code của em theo như sau nhé:
    PHP:
    $categoryId 10;
    $post_id 200;
    $args=array(
      
    'category__in' => $categoryId,
      
    'post__not_in' => array($post_id),
      
    'posts_per_page'=> 2// Number of related posts that will be shown.
      
    'meta_query' => array( 'main_query' => array(
            
    'key' => 'add_to_home',
            
    'value' => '2:Yes'
        
    ), 'orderby_query' => array(
            
    'key' => 'stt_home',
        )
      ),
      
    'orderby' => array(
        
    'orderby_query' => 'ASC',
      )
      
    //'meta_key'      => 'add_to_home',
      //'meta_value'    => '2:Yes'
    );
    • Như trên là được ngay nhé