Wordpress query_posts avoid content - wordpress

I am creating a wordpress page to display the list of posts of a specific category.
I use the function query_posts in the following way:
$posts = query_posts(array('category_name'=>'formazione'));
My problem is that the function shows the post content even without the loop and redirect the output to the $posts variable.
How can I avoid displaying the post content?

Don't use the variable $posts to start with, as said in the comments to your question.
Ideally look at using get_posts() https://codex.wordpress.org/Template_Tags/get_posts instead to get your posts. There are examples at the bottom of that page on how to use it.
$args = array( 'category_name' => 'formazione' );
$myposts = get_posts($args);
Etc... look at the page for how to handle $myposts data that comes back.

Related

Search box on single Post page should only search for posts

I have written code in content-single.php It is searching pages also.I want to search only post when I am Searching from post
https://wordpress.stackexchange.com/q/147222/50884
From what i understand you need to have your WordPress search only search posts for results, you can insert this code before the ‘if(have_posts()):’ line on search.php and it will do that for you.
// search only posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
Further to that you can add any arguments from the standard WP_Query function to there to make the search more custom :)

Wordpress: retrieve first five items from all populated categories

I am trying to find a way to achive the following: I'd like to make a custom template. Within this template I'd like to list the name of each category on my site that has at least 1 item assigned to it. Beneath the category name (and link) I'd like to insert some content from the first x number of items that have that particular category assigned.
Just in case it makes a massive difference the items in question are not posts, but custom items.
Can anyone give me some pointers or help? I assume there will be something I can do with the wp_query function, but I'm not really sure how to inject it between each of the category titles, or indeed how to make it work with a category for which I can't explicitly provide an id in the code).
Thank you.
get_posts() is your ideal solution. For example:
<?php $posts_array = get_posts( $args ); ?>
<?php $args = array(
'posts_per_page' => 1,
'category' => $post_cat,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'yourcustompostname',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
///respective loop here ie : foreach $myposts as $mypost ... etc
?>
Where $post_cat is the id of your category and post_type is the custom post type.
To keep these 'items' organized I would create a custom post and then assign a category to those posts, for each respective category (if there are multiple). If there is just one, than you could live with just using a single category.
EDIT:
to get the categories assigned to this custom post take a look at this link
Using the results from this you could assign the cat id's to the variable created earlier: $post_cat.
You have to use query_posts function
query_posts(array('category_name'=>'category-slug','posts_per_page'=>'5'));
then you need to do
while ( have_posts() ) : the_post();
and you can get the post id using:
$p_id = get_the_ID();
for more information : query_posts
You can use get_categories()
http://codex.wordpress.org/Function_Reference/get_categories
But, then you need to find out where the data for 'items' is stored, and query depending on that.
Here is the answer of your problem. You need to change the category id of own post category id .
Here is the code. just copy and past it.
$query1 = new WP_Query( array( 'post_type' => 'post','cat' =>'10,9') );
Use the above query in loop and after loop,reset your query . see the below code.
wp_reset_query();
Please use it and let me know if needs anything else.

How to get pagination working with “Pending” Posts

I'm working on a site that allows users to browse through pending posts/content on the front-end.
However, I can't seem to get pagination working with those posts. I've got a custom query that brings up the first page of pending posts on a category page, archive, etc.
But the Page 2, 3, etc. doesn't work.
Any thoughts?
Thanks!
Here's the example code I'm working with:
$args = array(
'cat' => $cat_ID,
'paged' => get_query_var('paged'),
'showposts' => 50,
'posts_per_page' => 50,
'post_status' => 'pending',
);
query_posts($args);
if( have_posts() ) : while (have_posts()) : the_post();
//Post code inserted here
<?php endwhile; ?>
<?php endif; ?>
WordPress pagination will 404 if there are not enough results in the main query to run to that page.
I'm sure there is a better way, but the only way I can think of round it is to use your custom loop in an archive/search page that has more posts than you have drafts.
For example, add your custom loop to the search.php template and reach it by passing a search query that will generate lots of results (e.g. 'a'). Using search.php will include pages, posts and custom post types in the results so will yield more results.
Use paginate_links for the pagination, which will continue to pass the queryvar on each page and after that you should be good.
On a slightly separate note, I would suggest using WP_Query instead of query_posts. See here for why.

Wordpress get_posts (show all attachments) pagination outside of the loop

On one of my Wordpress pages (which is really an image blog site) I'm using masonry.js with the Wordpress function get_posts to dump all attachments to my blog posts and display them in a grid. This works fine. However, there's obviously a lot of images and I was hoping to use the infinitescroll.js with this. The only problem is that the get_posts function, outside the loop, doesn't retain the pagination and therefore the functionality of infinitescroll.js doesn't work.
Here is the code I am using to dump all the attachments:
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_attachment_link($post->ID, true);
the_excerpt();
}
}
?>
Is there anyway of adding in pagination to the original Wordpress get_posts() attachment dump outside of the loop, or can anyone think of a solution?
I've done something similar using the 'offset' parameter for get posts.
Basically with each new call to get posts, simply increase your offset amount by the amount of new thumbnails you want to display each time. When the number of thumbnails returned is less than your offset amount, you have reached the end of your posts.
Another solution is to use the pagination parameters of the Wp_Query class. See here for what these are.

enabling pagination inside wordpress post

i have a specific single page which display a single post.
the thing is that i want to display below it all the other posts which have the same special meta data and i made that to work as well.
the problem starts when i try to make pagination to the list of the posts below.
the single post url is something like that:
blog.com/somepost
and the pagination link to the second page of posts below looks someting like this
blog.com/somepost/page/2
and wordpress automatically redirects me back to
blog.com/somepost
how can i prevent it from redirecting me back?
btw, i"m using something like that:
i"m doing something like this:
while( have_posts() ): the_post();
//here printing the single post
endwhile;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_btree_project_id',
'meta_value' => $post->ID,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
while( have_posts() ): the_post();
//looping through the related posts here
endwhile;
The reading I've done about WordPress pagination gives me the impression that it is an imperfect feature. It requires the global var $wp_query which stems from the WP_Query object. WP_Query holds the global $wp_query which is necessary for making even basic pagination work. Custom queryies don't have access to $wp_query, nor do they own a var to control pagination. I assume you are using a custom query to grab that single post, and as this article points out, with custom queryies:
the “fix” is to trick WordPress into
using the global $wp_query variable
when using our own custom loops.
The article gives an example of utiilizing the global var in your custom query so that you have access to the query_vars that make pagination possible.
I expect that your permalink structure and a custom query that I'm guessing you are using might not be working because the global $wp_query var isn't available during your loop to show related posts.
What does your code to get, display, and paginate the related posts look like? Can you post?

Resources