DIY: Show only specific posts on block and sliders / filter blocks by post ID

Posted in: Newspaper
Post count: 64

Hi,

posting this for those who might need this too and also for those who know way better than me, so that they hopefully have a check at this and spot eventual flaws or ways to improve.

this came up along with the need I had to mix different post types in the same slider. On my front page I have a slider that shows my selection of featured posts. I used to achieve this in NP4 using revslider, which allows to select specific posts to show up. But I decided to drop revslider plugin in NP6 so I made a little change to allow me to specify a group of posts to show up in a block.

– Works for blocks and slider (might work with grids and other elements but didn’t test with them)
– Does not work with pagination or ajax filter. I just wanted to show 4-6 posts so I didn’t need pagination and didn’t care to make it in a way that pagination would work. And there’s nothing to filter when you individually select all the posts shown. This means it WILL NOT WORK ON BLOCK THAT HAVE PAGINATION BY DEFAULT(like block 15 for example)

1. Add the filter input field to the block settings panel.
add this to file Newspaper\includes\td_config.php

		array(
				"param_name" => "pst_ids",
				"type" => "textfield",
				"value" => '',
				"heading" => 'Post ID filter:',
				"description" => "Filter multiple posts by ID. Enter here the post IDs separated by commas (ex: 13,23,18)",
				"holder" => "div",
				"class" => "",
                'group' => $group
			),

img1

2. Make the theme read that filter input
add this to file Newspaper\includes\wp_booster\td_data_source.php
'pst_ids' => '',
img2

3. Send that filter to the query to populate the block with the desired posts
add this to file Newspaper\includes\wp_booster\td_data_source.php

        if (!empty($pst_ids)) {

            $wp_query_args['post__in'] = explode (',', $pst_ids);
        }

img3

Note: if you wish to mix up regular posts and custom posts types, don’t forget to insert them in the Post Type filter.

Cheers!

  • This topic was modified 11 years by nf_prt.
  • This topic was modified 11 years by nf_prt.
  • This topic was modified 11 years by Radu.
Post count: 780

Hi!

Thank you so much for this tutorial! We are working to make a “best of” section here on the forums.

I’ve added this suggestion on our issue tracker and if we find a way to implement it without affecting existing functionality we will implement it in the theme.

We are also investigating the issue with core theme files + child theme. It would be nice if the files could be overwritten by the child theme.

Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.