Skip to content

Instantly share code, notes, and snippets.

@otakupahp
Last active August 13, 2024 10:04
Show Gist options
  • Save otakupahp/48f3b5d76f1283b99226b071ae378b36 to your computer and use it in GitHub Desktop.
Save otakupahp/48f3b5d76f1283b99226b071ae378b36 to your computer and use it in GitHub Desktop.
Run Jetpack Instant Search locally
-- Replace table name as needed
SELECT * FROM `wp_options` WHERE `option_name` LIKE '%jetpack_search%' ORDER BY option_name ASC;
<?php
// If we are on localhost, force Jetpack online
if ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
add_filter( 'jetpack_offline_mode', '__return_false' );
}
@otakupahp
Copy link
Author

otakupahp commented May 6, 2022

REQUIREMENT

  • Jetpack Instant Search must be active in a staging site, this won't work if you use a Live version
  • Jetpack installed in your local environment

Run the SQL in your staging database, you should get something like this:
imagen

Copy the options jetpack_search_plan_info and jetpack_search_ever_supported_search into your local database

Note: If you can't access the staging database but have the SQL, you can do a manual search with a text editor, or import the DB locally and look for that data there.

Add the PHP filter at your functions.php file, inside a plugin or a mu-plugin.

If you access to the Jetpack dashboard, and you won't see the Search menu, you could access manually by adding admin.php?page=jetpack-search to the wp-admin path

To configure the search, paste admin.php?page=jetpack-search-configure to the wp-admin path

@verytwisty
Copy link

verytwisty commented Aug 9, 2024

@otakupahp I have been using this on my current project!

I have a tip if you want to be able to style the JP related posts block on locally, you can do this:

function create_related_posts_array( $related_posts ) {
	$latest = new WP_Query(
		array(
			'orderby'        => 'rand',
			'posts_per_page' => 3,
			'post_type'      => 'post',
		)
	);

	$new_posts = array();

	foreach ( $latest->posts as $post ) {
		$new_posts[] = array(
			'title'  => $post->post_title,
			'url'    => get_the_permalink( $post->ID ),
			'rel'    => '',
			'id'     => $post->ID,
			'img'    => array(
				'src'      => get_the_post_thumbnail_url( $post->ID, 'medium' ),
				'alt_text' => get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true ),
				'srcset'   => wp_get_attachment_image_srcset( get_post_thumbnail_id( $post->ID ), 'medium' ),
			),
			'date'   => get_the_date( '', $post->ID ),
			'author' => get_the_author_meta( 'display_name', $post->post_author ),
		);
	}

	return $new_posts;
}

add_filter( 'jetpack_relatedposts_returned_results', 'create_related_posts_array' );

This will give you some posts to work with...

Also if you want to modify the HTML output you can add this filter:

functionfilter_jp_replated_posts_output( $display_markup, $post_id, $related_posts, $block_attributes ) {
// do stuff here
return $display_markup;
}
add_filter( 'jetpack_related_posts_display_markup', 'filter_jp_replated_posts_output', 10, 4 );```

If you need to make better markup for related posts, found by @chrisbellboy 

@otakupahp
Copy link
Author

Thanks for the tip @verytwisty. It's very useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment