Created
October 18, 2019 11:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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