Site logo

Archived topic

How to list explicit posts on explicit pages ?

33 replies · Started by Tim on September 29, 2021

Viewing posts 16–30 of 34

I'm not exactly sure how or where you've associated the ACF fields but the usage of wp_show_posts_shortcode_args would be something like this:

add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) {
$acf_post_listing_array = get_field('your_acf_slug');
    if (1234 === (int) $settings["list_id"]) {
        $args['post__in'] = $acf_post_listing_array;
    }
    return $args;
},10,2);

For the filter to work, you need to use the beta version of WPSP - https://wpshowposts.com/wp-show-posts-1-2-0/

This code assumes that the ACF field actually returns an array. If it's a simple list of strings then we may have to edit the code a bit to explode the field value to an array. :D

The ACF fields were created using the ACF plugin GUI.

And until now I was able to display using a hook that contains:

<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
	<h2>Featured Experience</h2>
    <ul>
    <?php foreach( $featured_posts as $featured_post ): 
        $permalink = get_permalink( $featured_post->ID );
        $title = get_the_title( $featured_post->ID );
        ?>
        <li>
            <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

Which works, but maybe I should continue with this method, since I'm not sure about working with a Beta at this stage.

Can you do a var_dump($featured_posts); to check what type of data the ACF field returns?

Which works, but maybe I should continue with this method, since I’m not sure about working with a Beta at this stage.

If you don't wish to use a beta version, you may have to modify the plugin's coding a bit.

You'll need to replace a line of code within the plugin which is this one - https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de

Which is basically passing $settings variable to the filter so we can specify the list ID. :)

Not sure where to put var_dump($featured_posts); since the output on the page get intermingled with other elements so does not come out clean.

Not sure where to put var_dump($featured_posts); since the output on the page get intermingled with other elements so does not come out clean.

You can temporarily add it after the if( $featured_posts ) and check it on the front end.

Strings of data should appear on the front-end once you've added this. It'll basically tell us what kind of data $featured_posts returns so we know how to use it as a query arg for WPSP. :D

Yes, this is what I did previously, but the output is intermingled with the other elements of the page, so might be a bit difficult to read. But check it out now.

I don't see the listing of posts. Perhaps that's not what you meant.

I was expecting an array with list of ids.

Example: array('1234','5678','9101','1121');

And this is quite important for the array because we basically have to do something like this:

Example: you want to have a listing for the IDs in this array = array('1234','5678','9101','1121');

You can do with something like this.

add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) {
    if (1234 === (int) $settings["list_id"]) {
        $args['post__in'] = array('1234','5678','9101','1121');
    }
    return $args;
},10,2);

But if I may suggest, I think we can skip all the coding.

Personally, I'd tag all the explicit post with a specific tag.

On a page, I will then add a WPSP that queries the the tag I've added to the explicit posts.

This is actually what David meant on his reply here - https://generatepress.com/forums/topic/how-to-list-explicit-posts-on-explicit-pages/#post-1946797

Alternative listing by post ID or by tags, you can also do a WPSP listing based on an ACF value.

You can basically create a boolean ACF field for each post marking it "explicit" true or false and then query it w/ the same filter.

Example: (Assuming you have a true or false check box on your post with a slug of explicit)

add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) {
    if (1234 === (int) $settings["list_id"]) {
        $args['meta_query'] = array(
			'relation' => 'AND',
            array( 
				'key' => 'explicit',
				'compare' => 'EXISTS'
			),
			array( 
				'key' => 'explicit',
				'value' => true
			),
		);
    }
    return $args;
},10,2);

Hello Elvin

Thank you for your thoughts.

You should be able to see this from the dump on the page. This is the output given when using the ACF Return Format=Post Object:

array(5) { [0]=> object(WP_Post)#6854 (24) { ["ID"]=> int(1570) ["post_author"]=> string(1) "4" ["post_date"]=> string(19) "2010-03-21 11:47:52" ["post_date_gmt"]=> string(19) "2010-03-21 11:47:52" ["post_content"]=> string(412) "

As you can see the Post ID is in there amongst all the other data.

If I switch the ACF field to Return Format=Post ID then you will see:

array(5) { [0]=> int(1570) [1]=> int(1588) [2]=> int(3876) [3]=> int(3879) [4]=> int(1624) }

Which is probably more what you are looking for.

The problem with the suggestion about tags is that it is not user-friendly. It requires a user to:
* create a client page
* remember to create a tag for that client
* put that tag on all the relevant posts
* ask someone to create a new WPSP to filter for that tag

The current method which I already have working only requires the user to
* create a client page
* click on the ACF box
* select the posts to add

Then the following code in a hook displays all the selected posts:

<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
	<h2>Featured Experience</h2>
    <ul>
    <?php foreach( $featured_posts as $featured_post ): 
        $permalink = get_permalink( $featured_post->ID );
        $title = get_the_title( $featured_post->ID );
        ?>
        <li>
            <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

I was wondering how to do the same thing with WPSP as proposed by David, but it sounds like it is not yet a fully supported method, or perhaps it is.

OK, so far I managed to get the array post title, image, and excerpt to display.

I expect the code is not following best practice.

One I couldn't figure out is how to display the date, can you see what is up there ?

<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
    <h2>Featured Experience</h2>
    <div>
    <?php foreach( $featured_posts as $featured_post ): 
        $permalink = get_permalink( $featured_post->ID );
        $title = get_the_title( $featured_post->ID );
	$size = 'medium';
	$post_date = 'Ymd';
	$post_excerpt = get_the_excerpt( $featured_post->ID );
        ?>
      <h3>
        <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
      </h3>
      <time>
	<?php echo get_the_date( $featured_post->ID, $post_date ); ?>
      </time>
      <figure>
	<?php echo get_the_post_thumbnail( $featured_post->ID, $size );?>
      </figure>
      <p>
	<?php echo $post_excerpt; ?>
      </p>
    <?php endforeach; ?>
    </div>
<?php endif; ?>

I got a bit further, displaying all the elements from the foreach

<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
 <h2>Featured Experience</h2>
 <div>
    <?php foreach( $featured_posts as $featured_post ): 
        $permalink = get_permalink( $featured_post->ID );
        $title = get_the_title( $featured_post->ID );
	$post_date = get_the_date( 'Y-m-d', $featured_post->ID );
	$post_image = get_the_post_thumbnail( $featured_post->ID, 'medium' );  	
	$post_excerpt = get_the_excerpt( $featured_post->ID );
    ?>
    <h3>
     <a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
    </h3>
    <time>
     <?php echo $post_date; ?>
    </time>
    <figure>
     <?php echo $post_image; ?>
    </figure>
    <p>
     <?php echo $post_excerpt; ?>
    </p>
   <?php endforeach; ?>
  </div>
<?php endif; ?>

But how do I order these posts by date ascending ?

But how do I order these posts by date ascending ?

That's actually the issue w/ doing it by foreach iteration. The order based on key order of the array items.

Which is why doing it with a post query(like using WPSP) is the ideal thing to do. Because this way you can use orderby to sort the post order by a specific parameter value.

In fact I think we can simplify the code to a simple wpsp_display() with $args containing only the post__in for your array and orderby => date if you want to sort it by date.

Example:

$settings = array(
    'post__in' => array('1234','5678','9101','1121'),
    'orderby' => 'date',
    'order' => 'ASC',
);

wpsp_display( 1234 , $settings );

Where you can change the array to $featured_posts since it seems to return an array. :D

Hello Elvin

Thank you for your continued support.

Yes, I would love to do this using the WPSP method, but it appeared that it was not possible to get the array of IDs from ACF in a current stable release version of WPSP, so I felt uncomfortable doing it with a Beta and I'm not sure if the Return Formats from ACF I posted above would be acceptable either.

Do I sound too timid about this approach ?

Thanks

...it was not possible to get the array of IDs from ACF in a current stable release version of WPSP

Not exactly sure what you mean by this.

Did you mean something like this isn't working?

$featured_posts = get_field('featured_posts');

$settings = array(
    'post__in' => $featured_posts,
    'orderby' => 'date',
    'order' => 'ASC',
);

wpsp_display( 1234 , $settings );

Hello Elvin

Following your lead, I created a new WPSP list selecting a single post category, and displayed it using the code you offered, but it seemed unnecessary to use 'orderby' => 'date', 'order' => 'ASC' when the options are already available in the WPSP "More settings" so it could be done just using this:

$featured_posts = get_field('featured_posts');

$settings = array(
    'post__in' => $featured_posts,

);

wpsp_display( 4004 , $settings );

This displays a list of all the posts in the selected single category.

What should I edit to make sure only the posts from the ACF featured_posts field are displayed ?

Thanks

any chance you can provide us temporary backend access to have a look at how everything is set up?

You can use the private information text field to provide the details.

Generally, either wpsp_display() or the shortcode filter like this one should work:

add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) {
$featured_posts = get_field('featured_posts');
    if (4004 === (int) $settings["list_id"]) {
        $args['post__in'] = $featured_posts;
    }
    return $args;
},10,2);

Note: The filter works only on the latest beta version or if you did this modification on the current release - https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de

This archived topic is closed to new replies.