Php 在Wordpress中搜索自定义元字段

Php 在Wordpress中搜索自定义元字段,php,wordpress,Php,Wordpress,我的侧边栏中有一个高级搜索表单。该操作设置为get_bloginfo('home'),因此它应该指向search.php(因为表单的id是“searchform”) 该搜索表单应按自定义元字段过滤结果 我尝试并创建了这个search.php <?php function get_reviews_by_custom_search() { global $wpdb; $grad = preg_replace( '/^[0-9a-zA-Z-]/', '',

我的侧边栏中有一个高级搜索表单。该操作设置为get_bloginfo('home'),因此它应该指向search.php(因为表单的id是“searchform”)

该搜索表单应按自定义元字段过滤结果

我尝试并创建了这个search.php

<?php
function get_reviews_by_custom_search() {
    global $wpdb;

    $grad           = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['grad'] );
    $adType         = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['adType'] );
    $realEstateType = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['realEstateType'] );
    $dioGrada       = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['dioGrada'] );
    $squareFrom     = preg_replace( '/[^0-9]/', '', $_GET['squareFrom'] );
    $squareTo       = preg_replace( '/[^0-9]/', '', $_GET['squareTo'] );
    $priceFrom      = preg_replace( '/[^0-9]/', '', $_GET['priceFrom'] );
    $priceTo        = preg_replace( '/[^0-9]/', '', $_GET['priceTo'] );
    $roomsNum       = preg_replace( '/[^0-9]/', '', $_GET['roomsNum'] );

    // Change the defaults if not chosen
    if($squareFrom == '') { $squareFrom = '0'; }
    if($squareTo == '') { $squareTo = '10000000'; }

    // Define the arguments for the WP query
    $args = array(
            'post_type' => 'post',
            'relation' => 'AND',
            'meta_query' => array(
                    array(
                            'key' => 'ex_lokacija',
                            'value' => $grad ,
                            'compare' => 'LIKE'
                    ),
                    array(
                            'key' => 'ex_vrsta_oglasa',
                            'value' => $adType ,
                            'compare' => 'LIKE'
                    ),
                    array(
                            'key' => 'ex_tip_nekretnine',
                            'value' => $realEstateType ,
                            'compare' => 'LIKE'
                    ),
                    array(
                            'key' => 'ex_dio_pg',
                            'value' => $dioGrada ,
                            'compare' => 'LIKE'
                    ),
                    array(
                            'key' => 'ex_dio_pg',
                            'value' => $dioGrada ,
                            'compare' => 'LIKE'
                    ),
                    array(
                            'key' => 'et_square_footage',
                            'value' => array( $squareFrom, $squareTo ),
                            'type' => 'numeric',
                            'compare' => 'BETWEEN'
                    ),
                    array(
                            'key' => 'et_price',
                            'value' => array( $priceFrom, $priceTo ),
                            'type' => 'numeric',
                            'compare' => 'BETWEEN'
                                    )
            )
    );

    $searched_posts = new WP_Query( $args );

    return $searched_posts;
}

$searched_posts = get_reviews_by_custom_search();       

     get_header(); ?>

        <div id="content-top">
            <div id="menu-bg"></div>
            <div id="top-index-overlay"></div>

            <div id="content" class="clearfix">
                <div id="main-area">
                    <?php get_template_part('includes/breadcrumbs'); 

                        foreach ($searched_posts as $searched_post) {

                            echo "<h1 class=\"entry-title\"><a href=\"" . get_permalink($searched_post->ID) . "\">" . $searched_post->post_title . "</a></h1>";

                            echo "Rating - " . get_post_meta($searched_post->ID,'rating',true) . "<br>";
                            echo "Audience - " . get_post_meta($searched_post->ID,'audience',true) . "<br>";
                            echo "Length - " . get_post_meta($searched_post->ID,'length',true) . "<br>";    

                            echo "<a href=\"" . get_permalink($searched_post->ID) . "\">Read More</a>";

                        }
                    ?>

                </div> <!-- end #main-area -->

                <?php get_sidebar(); ?>

    <?php get_footer(); ?>

我在网上找到了大部分代码,我还在学习PHP


第一部分是函数,它应该返回一个搜索帖子的数组。但是,部分代码是错误的。

这里有几个选项:

1.有一个不错的选择,你可以去看看

2、您可以捕获搜索词,然后在
WP\u查询中使用它:

$s = $_POST['search_field'];
$my_query = new WP_Query('meta_value='.$s);

欢迎来到这个网站!如果你在问题中加入了几行你知道或怀疑是错误的代码,那么就更容易得到好的答案。请记住,您可以使用`符号(在QWERTY键盘上1键的左侧)来包含您希望以不同方式显示的内容(如代码),以便它们突出显示。问题是,我在表单中有一些元素和单选按钮。该表单最多提交7个我分配给变量的值-您可以在我包含的文件顶部看到