PHP code example of devgeniem / redipress

1. Go to this page and download the library: Download devgeniem/redipress library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devgeniem / redipress example snippets


'geolocation'    => [
    'field'    => 'location',
    'compare'  => '<',
    'lat'      => 59.6,
    'lng'      => 16.5,
    'distance' => 100000,
],

'orderby' => [
    [
        'orderby' => 'location',
        'order'   => 'ASC',
        'compare' => [
            'lat' => 59.6,
            'lng' => 16.5,
        ]
    ]
]

new WP_Query([
    'post_type' => 'any',
    'orderby'   => 'relevance',
    'weight'    => [
        'post_type' => [
            'post'             => 1.5,
            'page'             => 3.0,
            'custom_post_type' => 7.0,
        ],
    ],
]);

new WP_Query([
    'post_type' => 'any',
    'orderby'   => 'relevance',
    'weight'    => [
        'author' => [
            'admin'      => 1.5,
            'jane_doe'   => 2.0,
            'john_smith' => 5.0,
        ],
    ],
]);

new WP_Query([
    'post_type' => 'any',
    'orderby'   => 'relevance',
    'weight'    => [
        'taxonomy' => [
            'category' => [
                'news'          => 3.0,
                'announcements' => 5.0,
            ],
            'post_tag' => [
                'some_tag'    => 4.25,
                'another_tag' => 6.0,
            ],
        ],
    ],
]);

new WP_Query([
    'post_type' => 'any',
    'orderby'   => 'relevance',
    'weight'    => [
        'meta' => [
            'meta_key1' => [
                'foo' => 2.0,
                'bar' => 3.0,
            ],
            'another_key' => [
                'fizz' => 1.25,
                'buzz' => 3.0,
            ],
        ],
    ],
]);

new WP_Query([
    's'         => 'foobar',
    'post_type' => 'any',
    'fuzzy'     => 2,
]);

new WP_Query([
    's'         => 'foobar',
    'post_type' => 'any',
    'blog'      => [ get_main_site_id(), 2, 3 ],
]);

add_filter(
    'redipress/return_fields',
    function ( $return_fields ) {
        $return_fields[] = 'blog_id';

        return $return_fields;
    }
);

add_filter(
    'redipress/formatted_search_results',
    function ( $results, $raw_results ) {

        // Format the raw results into associative array.
        $formatted_raw_results = array_map( function ( $result ) {
            return \Geniem\RediPress\Utility::format( $result );
        }, $raw_results );

        foreach ( $results as $key => $result ) {
            $raw             = $formatted_raw_results[ $key ] ?? [];
            $result->blog_id = (int) $raw['blog_id'] ?? 0;
        }

        return $results;
    },
    10,
    2,
);

add_filter( 'redipress/index/posts/schema_fields', function( $fields ) {
    $fields[] = new \Geniem\RediPress\Entity\TextField([
        'name'     => 'my_field_name',
        'weight'   => 1.5,
        'sortable' => true,
    ]);

    return $fields;
}, 10, 1 );

add_filter( 'redipress/additional_field/my_field_name', function( $data, $post_id, $post ) {
    return get_post_meta( $post_id, 'my_field_name', true );
}, 10, 1 );

add_filter( 'redipress/post_object', function( $post ) {
    $post->fields = \get_fields( $post->ID );

    return $post;
}, 10, 1 );