Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP魔术场查询帖子_Wordpress - Fatal编程技术网

Wordpress WP魔术场查询帖子

Wordpress WP魔术场查询帖子,wordpress,Wordpress,我试图显示“products”类型的帖子标题,其中有一个自定义字段“product_category”(下拉菜单),值为“food” 当“食物”字段设置为键入“文本”时,一切正常。当我改变这个类型时 字段“dropdown”没有显示任何内容 为了创建自定义页面和自定义字段,我使用Wordpress中的Magic fields插件 代码如下: <?php global $wp_query; query_posts(array( 'post_type'

我试图显示“products”类型的帖子标题,其中有一个自定义字段“product_category”(下拉菜单),值为“food”

当“食物”字段设置为键入“文本”时,一切正常。当我改变这个类型时 字段“dropdown”没有显示任何内容

为了创建自定义页面和自定义字段,我使用Wordpress中的Magic fields插件

代码如下:

     <?php
    global $wp_query;
    query_posts(array(
        'post_type' => 'products',
          'meta_query'  => array(
                   array(
                    'key'    => 'product_category',
                    'value'  => 'food',
                    )
                ))    
);
    while(have_posts()) : the_post(); ?>
        <?php $key = get_post_meta($post->ID, 'title'); ?>
        <li <?php post_class(); ?>><a href="<?php the_permalink(); ?>"><?php if($key) { echo $key[0]; } else { the_title(); }; ?></a></li>
        <?php
    endwhile;
    wp_reset_query();
    ?>

使用meta_键和meta_值


尝试使用
元查询
。确保您尝试将
产品
产品
作为
post\u类型
参数

$args = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key' => 'product_category',
            'value' => 'food'
        )
    )
);
$query = new WP_Query( $args );

另外,不要使用
query\u posts

您需要将参数metacompare添加到数组
(meta\u compare=>'like')
,因为在表
wp\u postmeta
上,
键值
“a:1:{i:0;s:7:“Food”}”一样保存。

所以

您应更改为:

query_posts(array(
        'post_type' => 'products',
        'meta_key'    => 'product_category',
        'meta_value'  => 'food',
        'meta_compare' => 'like'
                ))    
);

您讨论了字段类型问题,但没有发布代码。如何生成字段?我使用Magic Fields插件创建了自定义页面类型。此帖子类型正在将“功能类型”设置为页面。是否检查了数据库
wp\u postETA
表应该为要在
meta\u键下列出的帖子设置适当的值。wp\u postETA值似乎可以。不幸的是,此代码不起作用。无论我输入什么“meta_key”和“meta_value”,Wordpress都会显示我所有的产品页面。
query_posts(array(
        'post_type' => 'products',
        'meta_key'    => 'product_category',
        'meta_value'  => 'food',
        'meta_compare' => 'like'
                ))    
);