$posts_not_included = array( get_the_ID() );
$args = array(
'posts_per_page' => 3,
'posts__not_in' => $posts_not_included
);
query_posts($args);
I have this code in a single-post page. The page showcases a post, and then I try to call query_posts to get the other posts. However, the post I'm currently on is shown, despite the ID being shown by get_the_ID(); is correct. Am I doing something wrong? Do I need to use the WP_Query class instead?
Your parameter is wrong change it
$posts_not_included = array( get_the_ID() );
$args = array(
'posts_per_page' => 3,
'post__not_in' => $posts_not_included // right argument is post not posts
);
query_posts($args);
Related
I am creating a blog and I have difficulty with pages in wordpress, in my case I would like to insert content (raw content) from two pages randomly in modal system while people browse the blog.
Example:
Current Page> Modal Box> Page01 content or Page02 Content
I know it's possible by the page id. but I would like to make it more dynamic by getting the contents of the pages by slugs or by title.
here's the code
<?php
$term = get_taxonomy( $slug );
$args = array(
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
'tax_query' => array( array(
'taxonomy' => $term,
'field' => 'slug',
'terms' => array('page-01', 'page-02'),
) )
);
$rand = new WP_Query($args);
if ($rand->have_posts()) {
while ($rand->have_posts()) {
$rand->the_post();
the_content();
}
}
?>
I know that removing taxonomy from query gets the contents of publish pages but, in my case, I need to get content from two specific pages
You can use post_name__in to filter by multiple slugs, eg:
$args = array(
'post_name__in' => array('page-slug-1', 'page-slug-2'),
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
);
You can find more details on the WP_Query documentation page.
Is this possible, I couldn't find the question asked anywhere on the web.
If you had a list of posts by their slug, example:
$postslugs = array("page-one","page-two","page-three");
And you wanted to create a single query to find all these specific pages:
$args=array(
'post__not_in' => array($post->ID),
'posts_per_page'=> -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'pagename' => $postslugs
);
$my_query = new wp_query( $args );
The result should come back with the three pages. This of course would not work the way it's written now. Is this possible?
If you take a look at the documentation for the WP_Query() class, specifically the Post & Page Parameters section, you'll see it has a handy parameter called post_name__in which takes an array of post_name (aka slugs). Also, are you sure you need to use post__not_in (and maybe posts_per_page since you're defining an array of slugs to get already?
$names = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'post_name__in' => $names
);
$my_query = new WP_Query( $query_args );
I would like to notice, that there is some issue with old WordPress versions where post_name it is not = to a post_slug.
For example if you try to create new post with title Customer.io it will create a post with name customer-io but slug will be customerio the same will be for this Pokéapi
The same will be if you change post slug after you have created the post.
So the best way is to check manually inside loop of your WP_Query.
$desired_array_of_slugs = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish'
);
$my_query = new WP_Query( $query_args );
Then
foreach($my_query as $post) {
if(in_array(sanitize_title($post->name)], $desired_array_of_slugs, true)){
//do ur staff
}
}
I've been working on showing a list of posts that users have NOT favoured using this wordpress plugin: https://en-gb.wordpress.org/plugins/favorites/
I've re-created a search that I can make into a custom shortcode, but I can't work out how to reverse it to show UNfavorited lists.
Also, I would like to show the title and have the title link to the page - can anyone help? This is what I have:
$favorites = get_user_favorites();
if ( $favorites ) :
$favorites_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__in' => $favorites
));
if ( $favorites_query->have_posts() ) :
while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
echo '<p>' . get_the_title( $favorite ) . '</p>';
endwhile;
endif;
wp_reset_postdata();
endif;
I wonder if you mean:
$favorites_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__not_in' => $favorites
));
where we use post__not_in instead of post__in ?
From the Codex:
post__not_in (array) - use post ids. Specify post NOT to retrieve. If
this is used in the same query as post__in, it will be ignored.
But note that the amount of returned posts could be large with posts_per_page as -1.
I'd like to be able to call the user-selected tag ID for the Jetpack Featured Content module, but I can't find a function or an object that holds that value.
I'm basically trying to filter featured posts out of a WP_Query with these arguments:
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => $recent_posts_count,
'tag__not_in' => array(
[HERE IS WHERE I WANT PHP TO TELL ME THE TAG ID]
),
'post_type' => array(
'post'
)
);
Check This one
<?php
global $post;
$tags = get_tags($post->ID);
$tagids = array();
foreach($tags as $tag) $tagids[] = $tag->term_id;
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => $recent_posts_count,
'tag__not_in' => $tagids,
'post_type' => array(
'post'
)
);
?>
Thanks
I know this is pretty late, but I found this post while looking for the same thing, even posted in the WordPress support forum, but figured it out eventually:
$featured_options = get_option( 'featured-content' );
$featured_name = $featured_options[ 'tag-name' ];
I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)