此参考很简单:https://codex.wordpress.org/Class_Reference/WP_Query
orderby应为meta_value
“meta\\u value”-请注意,“meta\\u key=keyname”也必须存在于查询中。
您的密钥名称应该是以下名称之一:CPU、OS、RAM。。。
您还可以考虑WP_Meta_Query
班https://codex.wordpress.org/Class_Reference/WP_Meta_Query
更新我得到了这个结果
Array
(
[0] => WP_Post Object
(
[ID] => 3233
[post_author] => 1
[post_date] => 2016-11-19 12:51:43
[post_date_gmt] => 2016-11-19 12:51:43
[post_content] => abc
[post_title] => Mac Air Pro 9G
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => mac-air-pro-3
[to_ping] =>
[pinged] =>
[post_modified] => 2016-11-19 12:52:19
[post_modified_gmt] => 2016-11-19 12:52:19
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://steve.com/laptop/mac-air-pro-1-copy/
[menu_order] => 0
[post_type] => laptop
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => WP_Post Object
(
[ID] => 3232
[post_author] => 1
[post_date] => 2016-11-19 12:46:50
[post_date_gmt] => 2016-11-19 12:46:50
[post_content] => abc
[post_title] => Mac Air Pro 8G
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => mac-air-pro-2
[to_ping] =>
[pinged] =>
[post_modified] => 2016-11-19 12:52:47
[post_modified_gmt] => 2016-11-19 12:52:47
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://steve.com/laptop/mac-air-pro-1-copy/
[menu_order] => 0
[post_type] => laptop
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => WP_Post Object
(
[ID] => 3231
[post_author] => 1
[post_date] => 2016-11-19 12:12:38
[post_date_gmt] => 2016-11-19 12:12:38
[post_content] => abc
[post_title] => Mac Air Pro 4G
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => mac-air-pro-1
[to_ping] =>
[pinged] =>
[post_modified] => 2016-11-19 12:52:57
[post_modified_gmt] => 2016-11-19 12:52:57
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://steve.com/?post_type=laptop&p=3231
[menu_order] => 0
[post_type] => laptop
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
这是单台笔记本电脑的内容。php文件:
<?php
/**
* The template for displaying all single posts.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package microformats
*/
get_header();
// WP_Query arguments
$args = array (
\'post_type\' => array( \'laptop\' ),
\'post_status\' => array( \'published\' ),
//\'s\' => \'atom\', // only if you need that
\'nopaging\' => true,
\'posts_per_page\' => \'-1\',
\'ignore_sticky_posts\' => true,
// order
\'orderby\' => \'meta_value\',
\'meta_key\' => \'ram\',
\'order\' =>\'DESC\',
/* you will need this only if
\'meta_query\' => array(
array(
\'key\' => \'ram\', // or os, or cpu
\'value\' => \'4\', // meaning 4G of ram
\'compare\' => \'>\', // meaning only biggar
\'type\' => \'NUMERIC\',
),
),
*/
);
// The Query
$query = new WP_Query( $args );
print_r($query->posts);
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( \'template-parts/content\', \'single\' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
<hr class="fat" />
<?php endwhile; // End of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>