I have looked at this thread and tried to implement the given code sample there;
//code snippet to mass update all posts
add_action('init','mass_update_posts');
function mass_update_posts(){
$all_posts = get_posts('numberposts=');
$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => $all_posts ) );
foreach ( $my_posts as $my_post ):
wp_update_post( $my_post );
endforeach;
}
I put the code in my footer.php but it doesn't seem to do anything? What am I missing? Initially I used;
$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => -1 ) );
But that didn't help either...
Let me see if I understand what you are trying to do with this code...
First every time someone loads the page the footer.php will fire so you want to mass update all of your posts with a loop of the post itself?
There about a million things wrong with what you are trying to do with this code.
Never add actions in a footer file they belong in the function.php file of a theme.
Your $all_posts variable is probably empty because you are sending a function expecting an array of arguments a string (please read the get_post() function documentation)
$all_posts is not an integer as you are using it the next line (the get_post() function returns a list of WP_Post objects.
Your loop goes through all your posts and updates them with the same post, changing nothing and effectively accomplishing nothing.
So I guess the real question is what exactly are you trying to accomplish?
Related
I am filtering custom post types using ajax. I am able to filter using single taxonomy but how to filter results with multiple taxonomies.
This is the query i tried:
Code:
$args=array('orderby'=>'date','post_status'=>'publish');
//sort by bank if isset
if(isset($_POST['bank']))
{
$args['tax_query']=array(
array(
'taxonomy'=>'banks',
'field'=>'id',
'terms'=>$_POST['bank']
)
);
}
if(isset($_POST['card_type']))
{
$args['tax_query']=array(
'relation'=>'AND',
array(
'taxonomy'=>'cardtype',
'field'=>'id',
'terms'=>$_POST['card_type']
)
);
}
$query=new WP_Query($args);
But it only shows result with filtering from one taxonomy not both.
I think you are working on right track but a small mistake in argument may cause result to be rendered wrong.
Instead of 'relation'=>'AND', use 'relation'=>'OR'
'relation'=>'AND' will fetch result when both taxonomy condition match, while 'relation'=>'OR' condition will compare with any of the taxonomy.
click here to view the WP Query arguments
Actually yout array formation is wrong for tax_query.
Please check updated code below.
If you don't use $args['tax_query']['relation']= 'AND', then also it will work for you.
Please have a try with below code snippet.Update you code with below snippet.
$args = array(
'orderby'=>'date',
'post_status'=>'publish'
);
//sort by bank if isset
if(isset($_POST['bank'])){
$args['tax_query'][]= array(
'taxonomy'=>'banks',
'field'=>'id',
'terms'=>$_POST['bank']
);
}
if(isset($_POST['card_type'])){
$args['tax_query']['relation']= 'AND';//you can remove this
$args['tax_query'][]= array(
'taxonomy'=>'cardtype',
'field'=>'id',
'terms'=>$_POST['card_type']
);
}
$the_query = new WP_Query( $args );
is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks
If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!
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.
I have changed the default permalink of my website to Custom permalink (/%post-name%)
Now all posts require slug values .
which is not present currently.A slug is automatically added when a post is published or updated if slug screen option is unable ,but in my case now i have unable the slug option for all post earlier this was disabled and Now I want to update each post nothing want to add or delete just want to update all posts .
Currently number of posts in database is 200000,
Please suggest any efficient query or any method so that my task can be accomplished.
Thanks,
Monika
Try this plugin, it should do the job:
http://www.jerrytravis.com/598/wordpress-plugin-to-generate-post-slugs
Also this chunk of code can do the job, but you have to add some limits to avoid the script to crash due to the max limit of execution:
// get all posts
$posts = get_posts( array ( 'numberposts' => -1 ) );
foreach ( $posts as $post )
{
// check the slug and run an update if necessary
$new_slug = sanitize_title( $post->post_title );
wp_update_post(
array (
'ID' => $post->ID,
'post_name' => $new_slug
)
);
}
Credit for the code:
https://wordpress.stackexchange.com/questions/46086/regenerate-slugs-from-title-of-posts
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.