Support » Plugins » Hacks » creating array of post IDs to exclude from custom loop

  • maxgx

    (@maxgx)


    i’m using Metaphor Shortcodes plugin to display posts (“post blocks” as they’re called in the plugin) in a widget area placed at the top of a custom loop, through the use of its shortcodes.

    shortcodes share most settings (thumbnail size, excerpt length, etc.), so i resorted to use Shortcode Exec PHP to simplify the shortcode to be used.
    a shortcode like:
    [mtphr_post_block id="774" thumb_size="full" excerpt_length="50" excerpt_more=""]
    now looks like:
    [post_this id="774"]

    in Shortcode Exec PHP i’ve this:

    extract(shortcode_atts(array('id' => ''), $atts));
    echo do_shortcode('[mtphr_post_block id="'.$id.'" thumb_size="full" excerpt_length="50" excerpt_more=""]');

    what i need now is a way to hide these posts from the loop that runs just below the sidebar.

    i’ve tried storing the posts’ IDs into an array, then test if the post ID in the loop matches one of those in the array.

    i’ve created a function in functions.php to create a global the array:

    $arrayExcludedPosts = array();
    // Set a global array for posts to exclude from custom loop
    function arrayExclude($val) {
    	global $arrayExcludedPosts;
    	$arrayExcludedPosts[] = $val;
    	return $arrayExcludedPosts;
    }

    then added a line in Shortcode Exec PHP to call the function and store the ID in the global array:

    arrayExclude($id);
    extract(shortcode_atts(array('id' => ''), $atts));
    echo do_shortcode('[mtphr_post_block id="'.$id.'" thumb_size="full" excerpt_length="50" excerpt_more=""]');

    then the IF statement in the loop:

    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
      <?php if (!in_array($post->ID, $arrayExcludedPosts)): ?>
        <?php get_template_part( 'content', get_post_format() ); ?>
      <?php endif; ?>
    <?php endwhile; ?>

    but apparently no ID is being added to the array.

    trying to print what the function returns shows an empty array:

    <?php print_r(arrayExclude()); ?>
    // output:  Array ( [0] => )

    i think i’m missing some crucial point… can anyone help me out with this?
    thanks in advance!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    In order to collect post IDs from a loop run through a shortcode, you need to hack the shortcode handler code. Look for a code line that looks like this:
    add_shortcode('mtphr_post_block', 'some_callback_name');

    Determine the actual function callback in the place of ‘some_callback_name’, then locate the function declaration. Examine the loop that is run here and decide the best way to collect post IDS.

Viewing 1 replies (of 1 total)
  • The topic ‘creating array of post IDs to exclude from custom loop’ is closed to new replies.