php 查询WP_QUERY中的高级自定义字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 查询WP_QUERY中的高级自定义字段相关的知识,希望对你有一定的参考价值。

https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

for WP_QUERY custom fields params
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

<?php
/*
This example shows arguments to find all posts where a custom field called 
‘color’ has a value of ‘red’ or ‘orange’ and another custom field called 
‘featured’ (checkbox) is checked.
*/
$posts = get_posts(array(
	'numberposts'	=> -1,
	'post_type'		=> 'post',
	'meta_query'	=> array(
		'relation'		=> 'AND',
		array(
			'key'	 	=> 'color',
			'value'	  	=> array('red', 'orange'),
			'compare' 	=> 'IN',
		),
		array(
			'key'	  	=> 'featured',
			'value'	  	=> '1',
			'compare' 	=> '=',
		),
	),
));


// query
$the_query = new WP_Query( $args );

<?php if( $the_query->have_posts() ): ?>
	<ul>
	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
		<li>
			<a href="<?php the_permalink(); ?>">
				<img src="<?php the_field('event_thumbnail'); ?>" />
				<?php the_title(); ?>
			</a>
		</li>
	<?php endwhile; ?>
	</ul>
<?php endif; ?>

<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>


<?php
/*
 Multiple custom field values (text based values) 
*/

$type = ( isset($_GET['type'] ) )  ? $_GET['type'] : '' ;
$location =  ( isset($_GET['location'] ) )  ? urlencode( $_GET['location'] ) : ''  ;
$size =  ( isset($_GET['size'] ) )  ? $_GET['size'] : '' ;

$args = array(
	'numberposts'	=> -1,
	'post_type'		=> 'projects',
	'meta_query'	=> array(
		'relation'		=> 'AND',
		array(
			'key'		=> 'project_type',
			'value'		=>  $type,
			'compare'	=> '='
		),
		array(
			'key'		=> 'project_location',
			'value'		=>  $location,
			'compare'	=> '='
		),		
		array(
			'key'		=> 'project_size',
			'value'		=>  $size,
			'type'		=> 'NUMERIC',
			'compare'	=> '<='
		)
	)
);    
    
$projects_query = new WP_Query ($args);
?>

<?php  if( have_posts() ) : while($projects_query->have_posts() ) : $projects_query->the_post(); ?>

   <div class="col-md-3 col-xs-4 portfolio-piece">
      <?php 
      // get the url id
        $thumbnail_id = get_post_thumbnail_id( );
        // get actual url
        $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
       ?>
      <p><a href="<?php the_permalink(); ?>">
         <img src="<?php echo $thumbnail_url[0]; ?>"></a></p>
        <h4><a href="<?php the_permalink(); ?>"><?php the_title() ; ?></a></h4>
   </div>

</div>

<?php endwhile; endif; ?>


<?php
/*
Multiple custom field values (array based values)
*/

// 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'
		)
	)
);


// query
$the_query = new WP_Query( $args );

?>
<?php if( $the_query->have_posts() ): ?>
	<ul>
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
		<li>
			<a href="<?php the_permalink(); ?>">
				<img src="<?php the_field('event_thumbnail'); ?>" />
				<?php the_title(); ?>
			</a>
		</li>
	<?php endwhile; ?>
	</ul>
<?php endif; ?>

<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>







以上是关于php 查询WP_QUERY中的高级自定义字段的主要内容,如果未能解决你的问题,请参考以下文章

在自定义分类模板中的 WP_Query 期间查询(2)ACF 发布对象

php 事件日历 - 使用WP_Query进行自定义查询

WordPress 自定义查询 WP_Query 所有参数

taxonomy.php中的WP Query会杀死标准循环

php 将重力表单文件上载到高级自定义字段WordPress中的图像/文件字段

php 允许在自定义程序中的窗口小部件中使用高级自定义字段的插件