Skip to content

Instantly share code, notes, and snippets.

@LeMiira
Last active July 19, 2024 09:49
Show Gist options
  • Select an option

  • Save LeMiira/1c45afb15d0099dbed14afe39c8dfc3d to your computer and use it in GitHub Desktop.

Select an option

Save LeMiira/1c45afb15d0099dbed14afe39c8dfc3d to your computer and use it in GitHub Desktop.

Revisions

  1. LeMiira revised this gist Jul 19, 2024. 1 changed file with 33 additions and 8 deletions.
    41 changes: 33 additions & 8 deletions query.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,36 @@
    add_action('elementor/query/readNextQuery', function ($query) {
    // Retrieve the custom field 'what_to_read_next_articles' which contains the IDs of posts to display

    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);

    $query->set( 'posts_per_page', 3 );

    $query->set( 'posts__in', $postsID );


    // 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);
  2. LeMiira revised this gist Jul 17, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion query.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    $query->set( 'posts_per_page', 3 );

    $query->set( 'post__in', $postsID );
    $query->set( 'posts__in', $postsID );



  3. LeMiira created this gist Jul 17, 2024.
    11 changes: 11 additions & 0 deletions query.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    add_action('elementor/query/readNextQuery', function ($query) {
    // Retrieve the custom field 'what_to_read_next_articles' which contains the IDs of posts to display
    $postsID = get_post_meta(get_the_ID(), 'what_to_read_next_articles', true);

    $query->set( 'posts_per_page', 3 );

    $query->set( 'post__in', $postsID );



    });