1

I have a normal WP query that will output to JSON to fill out Google maps location data after search. What I'm trying to do next is also output the posts with title/thumbnail/link beside the map with the same query. How can I use the same query to output 2 variables - JSON = $locations and normal post output $postloop?

Function

<?php
function my_search(){
    $args = array(
        'orderby' => 'date',
        'order' => $_POST['date'],
        'post_type' => 'property',
        'posts_per_page' => 20,
        'date_query' => array(
            array(
                'after' => $_POST['property_added']
            ),
        ),
    );

    $query = new WP_Query( $args );
    if($query->have_posts()){
        while($query->have_posts()){
            $query->the_post();

            $locations[] = array(
                "name" => get_the_title(),
                "lat" => get_field('loc_lat'),
                "lng" => get_field('loc_lng')

            );
        }
    }
    //Return the data as JSON
    echo json_encode( $locations);
    die();

    //**Return normal query here to show post title/thumbnail excerpt etc **

}
add_action( 'wp_ajax_nopriv_my_search', 'my_search' );
add_action( 'wp_ajax_my_search', 'my_search' );

UPDATE 1

Ajax.js

jQuery(function($){
    $('#filter').submit(function(e){
        e.preventDefault();//preventing form to submit with page reload
        var filter = $('#filter');

        $.ajax({
            url: filter.attr('action'),
            data:filter.serialize(),
            async:false,
            type: 'POST', // POST
            dataType: 'json',
            success: function(response) {
                $('#response').html(response.posts);
                showCloseLocations(response.data);
                
             };
        });
        return false;
    });

WP QUERY

$posts =  ‘#the post query output for posts’;
$locationmap = json_encode( $locations );
$response = array(‘posts’=>$posts, 'data'=>$locationmap);
echo json_encode($response);
die();

JSON OUTPUT

{html: "#the post query output for posts", data: "[{\"name\":\"Test property\"…e\",\"lat\":\"xx.xxxxxx\",\"lng\":\"-x.xxxxxxxxxxx\"}]"}

The JSON should be outputting as this:

[{name: "Test property", lat: "x.xxxxxxxx", lng: "-x.xxxxxxxx"}, {name: "Property 3", lat: "xx.xxxxxxxx", lng: "-x.xxxxxxxx"}]
  • The REST endpoint in your last question didn't help? By the way why can't you do this? You are already outputting the post's title. – Jack Johansson Jul 4 '17 at 13:40
  • I suppose I could include the title, link and thumbnail within the endpoint but worried for larger JSON responses this could cause unnecessary load. Id like to be able to individually output the JSON to $locations and the other to output the custom posts with title/link/thumbnail etc. – scopeak Jul 4 '17 at 15:41
  • 1
    Once you run the query, it doesn't matter if you include title/thumbnail or not. WordPress already did the heavy task of querying the database, so the primary load is lifted. – Jack Johansson Jul 4 '17 at 16:34
  • 1
    Good point! I'll just do that then I think. Thanks for the response :) – scopeak Jul 4 '17 at 16:36
  • 1
    You're welcome. I actually had some similar question like this, you may find it useful in here. – Jack Johansson Jul 4 '17 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.