1

I need to be able to test which page is currently being displayed from within functions.php. This is so that I can apply a different filter to my posts based on which page is being displayed.

For example I have two pages one called buy.php and the other sold.php. I need to be able to target some kind of unique id for each of these pages when they are currently displayed and then use a if statement to apply different filters.

How I want my code to work;

if(page = buy.php){
    $posts['meta_query'][] = array(
        'key'       => 'property_status',
        'value'     => 'For Sale'
    );
}else if(page = sold.php){
    $posts['meta_query'][] = array(
        'key'       => 'property_status',
        'value'     => 'Sold'
    );
}

How can I do this?

I need to the code to work within this function, which is filtering my posts;

add_action('wp_ajax_customfilter', 'property_filter');
add_action('wp_ajax_nopriv_customfilter', 'property_filter');

function property_filter(){
    $posts = array(
        'posts_per_page'    =>  -1,
        'post_type'         =>  'property',
        'orderby'           =>  'date'
    );

    $posts['meta_query'] = array( 'relation' => 'AND' );

    // price filtering

    if($_POST['min_price'] && !empty($_POST['min_price'])){
        $min_price = $_POST['min_price'];
    }else{
        $min_price = 0;
    }

    if($_POST['max_price'] && !empty($_POST['max_price'])){
        $max_price = $_POST['max_price'];
    }else{
        $max_price = 10000000;
    }

    $posts['meta_query'][] = array(
        'key'       => 'property_price',
        'type'      => 'NUMERIC',
        'value'     => array($min_price, $max_price),
        'compare'   => 'BETWEEN'
    );

    // bed filtering

    if($_POST['min_beds'] && !empty($_POST['min_beds'])){
        $min_beds = $_POST['min_beds'];
    }else{
        $min_beds = '1'; 
    }

    if($_POST['max_beds'] && !empty($_POST['max_beds'])){
        $max_beds = $_POST['max_beds'];
    }else{
        $max_beds = '9+'; 
    }

    $posts['meta_query'][] = array(
        'key'       => 'bedrooms',
        'value'     => array($min_beds, $max_beds),
        'compare'   => 'BETWEEN'
    );

    //location filtering

    if(isset( $_POST['location'] ) && $_POST['location']){
        $location = $_POST['location'];
        $location_val = stripslashes($location);

        $posts['meta_query'][] = array(
            'relation'  => 'OR',
            array(
                'key'       => 'street',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            ),

            array(
                'key'       => 'town',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            ),

            array(
                'key'       => 'county',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            ),

            array(
                'key'       => 'postcode',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            )
        );                          
    }

    // property type filtering
    if(isset( $_POST['type'] ) && $_POST['type']){
        $posts['meta_query'][] = array(
            'key'       => 'type_of_property',
            'value'     => $_POST['type'],
            'compare'   => 'IN'
        );
    }

    // secondary flash filtering
    if(isset( $_POST['flash_type'] ) && $_POST['flash_type']){
        $posts['meta_query'][] = array(
            'key'       => 'optional_category',
            'value'     => $_POST['flash_type'],
            'compare'   => 'IN'
        );
    }

    $query = new WP_Query( $posts );

    if( $query->have_posts() ): ?>  
        <?php while( $query->have_posts() ): $query->the_post() ?>
            <div class="col-sm-6 col-md-4 col-lg-3 post">
                <div class="shadowwrapper2">
                    <a href="<?php the_permalink(); ?>">
                        <?php  
                            $main_field = get_field('images');
                            $first_row = $main_field[0];
                            $img = $first_row['image'];
                            $img_med = $img['sizes']['medium'];
                        ?>

                        <div class="propertywrapper">
                            <img class="img-fluid gal_imgs" src="<?php echo $img_med ?>" alt="<?php $img['alt']; ?>"/>

                            <?php $secondary_flash = get_field('optional_category'); ?>

                            <?php if($secondary_flash == "Ideal First Time Buy"): ?>
                                <span class="second_flash">Ideal First Time Buy</span>
                            <?php elseif($secondary_flash == "Ideal Investment"): ?>
                                <span class="second_flash">Ideal Investment</span>
                            <?php elseif($secondary_flash == "Under offer"): ?>
                                <span class="second_flash">Under offer</span>
                            <?php endif; ?>
                        </div>

                        <div class="cornerflash">
                            <img src="<?php bloginfo('template_directory'); ?>/imgs/forsale.svg" alt="corner flash">
                        </div> 

                        <div class="propertyinfo">
                            <div class="row m-0">
                                <div class="col-6 p-0 mt-2"><?php the_field('type_of_property'); ?></div>
                                <div class="col-6 p-0 mt-2"><?php the_field('bedrooms'); ?> bedrooms</div>
                            </div>
                        </div>

                        <div class="streetpricewrapper">
                            <p class="streetname">
                                <?php the_field('street'); ?>, <?php the_field('town'); ?>
                            </p>
                            <p class="price">
                                £<?php the_field('property_price'); ?>
                            </p>    
                        </div>                              
                    </a>
                </div>
            </div>
        <?php endwhile; 
    wp_reset_postdata(); 
    endif; 
    wp_die();
}
Reece
  • 2,581
  • 10
  • 42
  • 90
  • Do you want current page id or current page template name? – dipmala Jun 11 '18 at 12:02
  • Either really I just need to be able to target the current page. Whichever works best. thanks – Reece Jun 11 '18 at 12:03
  • In pure PHP: `$_SERVER['PHP_SELF']` or `$_SERVER['SCRIPT_FILENAME']`. check the comments in http://php.net/manual/en/reserved.variables.server.php about using PHP_SELF. . I do not know if Wordpress has something. Note in your `if` statements, use `==` no `=` :-) – Nic3500 Jun 11 '18 at 12:06
  • 1
    check this helps https://stackoverflow.com/questions/4837006/how-to-get-the-current-page-name-in-wordpress – Sugumar Venkatesan Jun 11 '18 at 12:07

3 Answers3

1

You can achieve that by simple if statement to check the current page by name, wordpress have a function called is_page()

<?php
function currentpage()
{
    if (is_page('buy')) {
        $posts['meta_query'][] = array(
            'key' => 'property_status',
            'value' => 'For Sale'
        );

    } elseif (is_page('sold')) {
        $posts['meta_query'][] = array(
            'key' => 'property_status',
            'value' => 'Sold'
        );
    }
}
add_action('wp_head', 'currentpage');

Update :

based on your edited code you can use like :

function property_filter(){
    $posts = array(
        'posts_per_page'    =>  -1,
        'post_type'         =>  'property',
        'orderby'           =>  'date'
    );

    $posts['meta_query'] = array( 'relation' => 'AND' );

    // price filtering

    if($_POST['min_price'] && !empty($_POST['min_price'])){
        $min_price = $_POST['min_price'];
    }else{
        $min_price = 0;
    }

    if($_POST['max_price'] && !empty($_POST['max_price'])){
        $max_price = $_POST['max_price'];
    }else{
        $max_price = 10000000;
    }

    $posts['meta_query'][] = array(
        'key'       => 'property_price',
        'type'      => 'NUMERIC',
        'value'     => array($min_price, $max_price),
        'compare'   => 'BETWEEN'
    );

    // bed filtering

    if($_POST['min_beds'] && !empty($_POST['min_beds'])){
        $min_beds = $_POST['min_beds'];
    }else{
        $min_beds = '1'; 
    }

    if($_POST['max_beds'] && !empty($_POST['max_beds'])){
        $max_beds = $_POST['max_beds'];
    }else{
        $max_beds = '9+'; 
    }

    $posts['meta_query'][] = array(
        'key'       => 'bedrooms',
        'value'     => array($min_beds, $max_beds),
        'compare'   => 'BETWEEN'
    );

    //location filtering

    if(isset( $_POST['location'] ) && $_POST['location']){
        $location = $_POST['location'];
        $location_val = stripslashes($location);

        $posts['meta_query'][] = array(
            'relation'  => 'OR',
            array(
                'key'       => 'street',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            ),

            array(
                'key'       => 'town',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            ),

            array(
                'key'       => 'county',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            ),

            array(
                'key'       => 'postcode',
                'value'     => $location_val,
                'compare'   => 'LIKE'
            )
        );                          
    }

    // property type filtering
    if(isset( $_POST['type'] ) && $_POST['type']){
        $posts['meta_query'][] = array(
            'key'       => 'type_of_property',
            'value'     => $_POST['type'],
            'compare'   => 'IN'
        );
    }

    // secondary flash filtering
    if(isset( $_POST['flash_type'] ) && $_POST['flash_type']){
        $posts['meta_query'][] = array(
            'key'       => 'optional_category',
            'value'     => $_POST['flash_type'],
            'compare'   => 'IN'
        );
    }

    //page type filtering

        if (is_page('buy')) {
        $posts['meta_query'][] = array(
            'key' => 'property_status',
            'value' => 'For Sale'
        );

    } elseif (is_page('sold')) {
        $posts['meta_query'][] = array(
            'key' => 'property_status',
            'value' => 'Sold'
        );
    }

    $query = new WP_Query( $posts );
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
1

Using Page id

   global $post;
   $pageid=$post->ID;
  if ( $pageid=="2" ) {    // 2 = your page id
    $posts['meta_query'][] = array(
    'key'       => 'property_status',
    'value'     => 'For Sale'
    );
  }

 else if( $pageid=="3" ){ // 3 = your page id
    $posts['meta_query'][] = array(
    'key'       => 'property_status',
    'value'     => 'Sold'
   );
 }

Using template

  if (is_page_template( 'templates/buy.php' ) ) {   
    $posts['meta_query'][] = array(
    'key'       => 'property_status',
    'value'     => 'For Sale'
    );
  }

 else if(is_page_template( 'templates/sold.php' ) ){ 
    $posts['meta_query'][] = array(
    'key'       => 'property_status',
    'value'     => 'Sold'
   );
 }
dipmala
  • 2,003
  • 1
  • 16
  • 17
0

Use

$_SERVER['REQUEST_URI']

This will give the current executing script's name. TO get only the filename, this would work

echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);
Penguine
  • 519
  • 6
  • 19