简体   繁体   中英

Sort by ACF field in the products in my Custom Post Type in Wordpress

Could you please tell me how to sort my products(it is my custom post type) by the field(see picture) in ACF ?

在此处输入图片说明

Here is my custom field code:

    /*******            *******/
/*   CUSTOM POST TYPE     */
/*******            *******/

// Custom Post Type - product
function register_post_product() {
  $labels = array(
    'name'               => __( 'Products', '_tk' ),
    'singular_name'      => __( 'Product', '_tk' ),
    'add_new'            => __( 'Add product', '_tk' ),
    'add_new_item'       => __( 'Add New product', '_tk' ),
    'edit_item'          => __( 'Edit product', '_tk' ),
    'new_item'           => __( 'New product', '_tk' ),
    'all_items'          => __( 'All products', '_tk' ),
    'view_item'          => __( 'View product', '_tk' ),
    'search_items'       => __( 'Search product', '_tk' ),
    'not_found'          => __( 'No product found', '_tk' ),
    'not_found_in_trash' => __( 'No product found in the Trash', '_tk' ), 
    'parent_item_colon'  => '',
    'menu_name'          => __( 'Products', '_tk' ),
  );
  $args = array(
    'labels'            => $labels,
    'hierarchical'      => true,
    'supports'          => array( 'title', 'editor', 'page-attributes', 'thumbnail' ),
    'public'            => true,
    'show_ui'           => true,
    'show_in_menu'      => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive'       => true,
    'rewrite'           => array('slug' => 'produkty','with_front' => false),
    'menu_position'     => 6,
    'menu_icon'         => 'dashicons-hammer'
  );
  register_post_type( 'product', $args ); 
}
add_action( 'init', 'register_post_product' );

I made some search in google and I tried to sort it like that:

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
      $query->set( 'orderby', 'meta_value_num' );
      $query->set( 'order', 'ASC' );
      $query->set( 'meta_query','capacity');

      return $query;
}

But with no results.

try this below function for pre_get_posts in wordpress

<?php
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {

  $query->set( 'post_type', 'product' );
  $query->set( 'meta_key', 'capacity' );
  $query->set( 'orderby', 'meta_value' ); // meta_value_num or meta_value
  $query->set( 'order', 'ASC' );


  return $query;
}
?>

Most likely the problem is $query->set( 'meta_query','capacity');

meta_query should be an an array, like

array( array('key' => 'capacity', 
             'value' => array(0, 1000), 
             'compare' => 'BETWEEN',
             'type' => 'numeric', 
     ));

https://codex.wordpress.org/Class_Reference/WP_Query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM