链接到一个wordpress页面,其中文章通过自定义字段数据过滤


Linking to a wordpress page where articles are filtered by custom field data

我必须链接到wordpress的存档页面,只显示一些特定的"meta_value"的帖子。在我的模板中的"archive.php"只寻找"类别","日","月"等,但我需要有筛选自定义字段的"meta_value"的可能性。我在另一个网站上看到过:

" www.mywebsite.com/cities/?城市= seelbach "

(浏览器的地址栏),我试了一下,但没有任何反应。

这是我的循环:

    <?php
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query(); 
        $wp_query->query('&showposts=6'.'&paged='.$paged); 
    ?>
    <?php $count = 0; ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $count++; ?>
        <li class="cf">
            <div class="jobs-stream-leftcol">
                <h2><?php the_title(); ?></h2>
                <?php echo get_post_meta($post->ID, 'description', true); ?>
                <br />
                <strong>Wo:</strong> <?php echo get_post_meta($post->ID, 'zip', true); ?>, <?php echo get_post_meta($post->ID, 'city', true); ?>
                <br />
                <strong>Frei ab:</strong> <?php echo get_post_meta($post->ID, 'date', true); ?>
            </div>

            <div class="jobs-stream-rightcol">
                <a href="#" class="jobs-stream-link-go">› Jetzt bewerben</a><br />
                <a href="<?php the_permalink() ?>">› Mehr Info</a>
            </div>
        </li>
    <?php endwhile; ?>        
    </ul>

加小改动

   <?php
        $cityvar = $_POST["city"];
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query(); 
        $wp_query->query('&showposts=6'.'&paged='.$paged); 
    ?>
    <?php $count = 0; ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $count++; 
    if (get_your_custom_meta_Value_here() == $cityvar){
    ?>
        <li class="cf">
            <div class="jobs-stream-leftcol">
                <h2><?php the_title(); ?></h2>
                <?php echo get_post_meta($post->ID, 'description', true); ?>
                <br />
                <strong>Wo:</strong> <?php echo get_post_meta($post->ID, 'zip', true); ?>, <?php echo get_post_meta($post->ID, 'city', true); ?>
                <br />
                <strong>Frei ab:</strong> <?php echo get_post_meta($post->ID, 'date', true); ?>
            </div>

            <div class="jobs-stream-rightcol">
                <a href="#" class="jobs-stream-link-go">› Jetzt bewerben</a><br />
                <a href="<?php the_permalink() ?>">› Mehr Info</a>
            </div>
        </li>
    <?php
    }
    endwhile; ?>   

您必须深入研究一下代码。假设您有一个查询字符串:

//Get the query string
$myMetaValue = $_GET['myMetaKey']; 
//Set Up your arguments including the metaValue
$args = array(
  'post_type' => 'any',
  'meta_query' => array(
   array(
    'key' => 'myMetaKey',
    'value' => $myMetaValue
  )
)
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) { ...

希望这对你有帮助!