dousui6488 2015-09-01 08:36 采纳率: 100%
浏览 44

使用自定义字段的下拉菜单查询帖子

I have created this category archive which contains a table of 100 posts. The table contains 3 custom fields (Gender,Continent,Sector) and there is also a fourth ('Overall' with the only value 'All') which is not inside the table and their values for each post are inside the table cells of every entry/post. I have placed above the table four select tags (the four drop down menus) which I would like to filter the posts depending on which option of a certain select tag is chosen. To be more specific when a user choses for example select tag 'Continent' and option 'Europe' I would to query all posts with custom field value 'Europe'. Is it possible to do that by using an if statement and how exactly would I syntax that? The select tags are the following:

For custom field Overall

<select>
<option value="all">All</option>
</select> 

For custom field Gender

<select>
<option value="gender">Select Gender</option>
<option value="male" >Male</option>
<option value="female">Female</option>
</select>

For custom field Continent

<select>                    
<option value="continent">Select Continent</option>
<option value="europe">Europe</option>
<option value="america">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option></select>

For custom field Sector

<select> 
<option value="sector">Select Sector</option>
<option value="retail">FMCG/Retail</option>
<option value="various">Various</option>
<option value="finance">Finance</option>
<option value="se">SE</option>
<option value="energy">Energy</option>
<option value="education">Education</option>
<option value="fb">F&B</option>
<option value="environment">Environment </option>
<option value="telco">Tech/telco </option>
<option value="fashion">Fashion </option>
<option value="media">Media </option>
<option value="construction">Construction </option>
<option value="health">Health </option>
<option value="tech">Tech </option>
<option value="industry">Industry </option>
</select>
  • 写回答

1条回答 默认 最新

  • doushuo1080 2015-09-01 09:45
    关注

    Try this one to query post by meta key and value

    <form name="res" action="" method="post">
    <select name="gender">
    <option value="gender">Select Gender</option>
    <option value="male" >Male</option>
    <option value="female">Female</option>
    </select>
    <input type="submit" name="submit" value="submit">
    </form>
    <?php
    if (!empty($_POST['submit'])) :
    $result = query_posts( array(
        'post_type' => 'your post type name',
        'meta_query' => array(
            array(
                'key' => 'gender',
                'value' => 'male',          
            ),
            array(
                'key' => 'continent',
                'value' => 'USA',
            ),
            array(
                'key' => 'all',
                'value' => 'all',
            )
        )
    ) );
    
    print_r($result);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探