Exclude Current and Sticky Post - wordpress

On a single Post page I have a side bar displaying up to three other, related posts. How can I exclude both Sticky Posts and the Current post?
I know how to exclude the Current post and how to exclude Sticky Posts by using post_not_in in a WP_Query, see code example below. But I guess you can not use post__not_in twice in the same query. Any suggestions?
$current_post_ID = get_the_ID();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' )
'post__not_in' => array($current_post_ID)
);

The post__not_in is an array() and uses post ids (numbers only).
There is no need to use it twice, use it like that:
//First build the array of excluded posts
$excluded_posts = array_push(get_option( 'sticky_posts' ) , $current_post_ID);
//Then use it in the option
'post__not_in' => array( $excluded_posts )
Note the array_push() PHP function, this will add the current post to the end of the sticky posts array, which is then passed to 'post__not_in' option.

<?php
$sticky =array(get_option('sticky_posts'));
// (add post id on sticky_posts option like ex. 485,458,256)
$current_post_ID = get_the_ID();
array_push($sticky,$current_post_ID);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => array($sticky)
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<!-- add your code which to display hear -->
<?php
endwhile;
wp_reset_query();
?>

Related

Get 5 random posts within current category in WordPress

I want to only get 5 posts within current category by random using the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
) );
How do you apply the '5 posts' limit and 'order by random' to the above code?
as for me, I would use get_posts(), but these arguments should work in your case as well:
<?php $args = array(
'numberposts' => 5,
'category' => $categories[0]->term_id,
'orderby' => 'rand',
'exclude' => array( get_the_ID() ),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
More about this here: https://codex.wordpress.org/Template_Tags/get_posts
I used the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'orderby' => 'rand',
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
) );

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

WordPress meta_query for Custom Post Type with orderby

I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;

WordPress: How to query some specific static pages along with the latest post of a certain category?

How to query some specific static pages along with the latest post of a certain category?
http://codex.wordpress.org/Class_Reference/WP_Query
for that you have to make a template.
and give that to specific page.
and in that template give below code to call posts of a specific category.
Try this:
<?php
$args2 = array(
'numberposts' => 1,
'cat' => 4,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$homepage_content = get_posts($args2);
foreach ($homepage_content as $key => $value) {
echo "<h2>" . $value->post_title . "</h2>";
echo $value->post_content;
$post_id = $value->ID;
}
?>
Here just pass ID of Category in cat argument.
For more reference: http://codex.wordpress.org/Template_Tags/get_posts
-
Thanks

WordPress query_posts display past dates

I have my page displaying only future dates which is just what I want, I have been digging around trying to find a way to query only: posts that are dated prior to todays date.
This is currently my query:
<?php
$args=array(
'post_type' => 'artists',
'orderby' => 'ecpt_featured_month',
'order' => 'ASC',
'post_status' => 'future'
);
Any ideas? If it helps, each post is an artists and they have a featured month, post = month.
Thanks,
Ryan
Use this one considering "ecpt_featured_month" is a custom field:
$args=array(
'post_type' => 'artists',
'orderby' => 'meta_value',
'meta_key' => 'ecpt_featured_month'
'order' => 'ASC',
'post_status' => 'future'
);
$loop = new WP_Query( $args);
while ( $loop->have_posts() ) : $loop->the_post();
// write the general code to display the post's title and date
endwhile;

Resources