Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save UraraReika/f7d1b138ca333152f6d53a48cc7ae1c3 to your computer and use it in GitHub Desktop.
Save UraraReika/f7d1b138ca333152f6d53a48cc7ae1c3 to your computer and use it in GitHub Desktop.
Get Post URL by meta value, function that returns a post URL that match the given meta value.
<?php
add_filter( 'jet-engine/listings/allowed-callbacks', '__your_prefix_get_post_url_by_post_meta_callback', 10, 2 );
function __your_prefix_get_post_url_by_post_meta_callback( $callbacks ) {
$callbacks['__your_prefix_post_meta_field_value'] = esc_html__( 'Post URL by Post Meta Value', 'jet-engine' );
return $callbacks;
}
function __your_prefix_post_meta_field_value( $meta_value ) {
global $wpdb;
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = %s", $meta_value ) );
if( count( $ids ) > 1 ) {
return false;
} else {
return get_permalink( $ids[0] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment