Aleš Sýkora / December 6, 2023 / 0 comments
Get only sticky posts in Bricks query builder
min read / Bricks Builder, Plugins, WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: If you need to query ONLY sticky (featured) WordPress posts with Bricks builder, here is how to do it. 1) You should know how it works in WP Query Here is how you construct WP Query to get sticky posts: 2) Use the Query editor in Bricks Builder Now you can use the knowledge in…
If you need to query ONLY sticky (featured) WordPress posts with Bricks builder, here is how to do it.
1) You should know how it works in WP Query
Here is how you construct WP Query to get sticky posts:
<?php
// arguments for query
$args = array(
'posts_per_page' => -1, // -1 = unlimited
'post__in' => get_option( 'sticky_posts' ), // only sticky posts
'ignore_sticky_posts' => 1,
);
// Creating a new WP_Query instance with $args
$query = new WP_Query( $args );
// Start the Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Display the title and content of each post
echo '<h2>' . get_the_title() . '</h2>';
the_content();
}
} else {
// If no sticky posts found
echo 'No sticky posts found.'; // Display a message
}
// Reset Post Data
wp_reset_postdata();
?>
2) Use the Query editor in Bricks Builder
Now you can use the knowledge in the Bricks builder.
- Turn on the Bricks builder’s Query editor
- Select Type = Posts
- Turn on the Query editor (PHP)
- Insert your Query $Args into the return PHP array
return [
'posts_per_page' => -1, // Change by your needs
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
];
3) Try it
Hurray! You are done. Time to test and edit your query args if you need.
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻