Last active
July 19, 2024 09:49
-
-
Save LeMiira/1c45afb15d0099dbed14afe39c8dfc3d to your computer and use it in GitHub Desktop.
elementor query post_in
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
add_action('elementor/query/readNextQueryx', function ($query) { | |
// Retrieve the custom field 'what_to_read_next_articles' | |
$postsID = get_post_meta(get_the_ID(), 'what_to_read_next_articles', true); | |
// Ensure $postsID is an array and contains only valid, existing post IDs | |
if (!is_array($postsID)) { | |
$postsID = explode(',', $postsID); | |
} | |
$postsID = array_filter($postsID, 'get_post'); | |
// Limit to 3 posts | |
$postsID = array_slice($postsID, 0, 3); | |
if (!empty($postsID)) { | |
// Set the query parameters | |
$query->set('post__in', $postsID); | |
$query->set('posts_per_page', count($postsID)); | |
$query->set('orderby', 'post__in'); | |
$query->set('ignore_sticky_posts', true); | |
$query->set('post_type', 'any'); // This will allow fetching any post type | |
// Ensure we're only getting published posts | |
$query->set('post_status', 'publish'); | |
// Remove any category or tag constraints | |
$query->set('category__in', []); | |
$query->set('tag__in', []); | |
} else { | |
// If no valid post IDs, return no results | |
$query->set('post__in', [0]); | |
} | |
// Remove the function if it exists to prevent double execution | |
remove_action('elementor/query/readNextQueryx', __FUNCTION__); | |
}, 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment