Random permalinks in Wordpress - wordpress

I wrote a code to retrieve random wp posts,It works perfectly the problem is it shows recently added media and its permalink.What changes to be made in the following code to retrieve random permalinks instead of recently added.
<?
function get_match( $regex, $content ) {
preg_match($regex, $content, $matches);
return $matches[1];
}
$shortcode_args = shortcode_parse_atts(get_match('/\[gallery\s(.*)\]/isU', $post->post_content));
$ids = $shortcode_args["ids"];
$attachments = get_posts(
array(
'include' => $ids,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'menu_order ID',
'orderby' => 'post__in',
)
);
foreach ($attachments as &$pos) {
$perm = get_permalink($pos->ID);
$img = wp_get_attachment_link( $pos->ID, 'thumbnail',true );
echo(''.$img.'');
}
?>

'post_type' => 'attachment', //retrives all attachements so change that to
'post_type' => 'post',// or your custom_post_type if you want to get only your custom post type
and also remove
'post_mime_type' => 'image',

Related

Wordpress - Delete custom-post-type post

I am trying to add a button in wordpress that deletes a custom-post-type post with a specific title and current user as author.
Problem that occurring is that all the posts gets deleted, all the job_alert posts, not only for this specific author or with this title.
Can someone see why?
$delete_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$posts = new WP_Query( $delete_post );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post( get_the_ID());
}
}
I also have this code that creates a post and that works great. Similar code.
$new_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$post_id = wp_insert_post( $new_post );
For deleting the particular post, here is the solution,
Please use name parameter instead of post_title in your query, then only it will return the required post which you want.
I have modified your code. Please find the updated code below:
$delete_post = array(
'post_type' => 'job_alert',
'name' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
Now, your required post will be returned.
Hope, this may be helpful to you and let me know, if you have any query.
Thanks.

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,
) );

why does this code just load 10 latest posts?

Someone made this code for me to load the posts in json format but it just loads the 10 latest posts. How do I change it to load all the posts?
I tried by?page=2 but it doesn't work.
json url
code
<?php
header("Content-type: application/json");
include ('wp-load.php');
$loop = new WP_Query(array( 'post_status' => 'publish', 'post_type' => 'post'));
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
$posts[] = array(
'id' => $post->ID,
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'guid' => $post->guid,
'image' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
'cats' => the_category_ID( false ),
'post_date' => $post->post_date,
);
endwhile; endif;
echo json_encode($posts);
?>
You should have taken a look at docs. There's a pre-set posts_per_page param which is to be set to -1 if you want all the posts:
$loop = new WP_Query(
array( 'post_status' => 'publish', 'post_type' => 'post', 'posts_per_page' => -1 )
);

Exclude parent posts and display only child posts in archive

I have done this query and is working.I have a lot of child posts and i plan to display only child posts when listing the archive page of my custom post type city-guide.
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
.....
}
I have tried
$all = get_posts(array('post_type'=> 'city-guide', 'posts_per_page' => -1));
$parents = array();
foreach ($all as $single)
{
$kids = get_children($single->ID);
if(isset($kids) && !empty($kids) && count($kids) >= 1)
{
$parents[] = $single->ID;
}
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'post__not_in' => $parents,
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
....
}
This did not work.Please help me find out where i went wrong.
I know it's an old question but hoping I can help someone that finds their way here looking for the same thing I was.
You can show ONLY child posts by excluding any posts with post_parent = 0 using the 'post_parent__not_in' argument:
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged,
'post_parent__not_in' => array(0)
);
This avoids the need to loop thru each parent post to get each child.
I see you are trying to push the IDs into an array but why not just use the IDs while you are looping through them while getting the children within the loop at the same time? The example below is how I would tackle this.
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
$i=1; while( $query->have_posts() ): $query->the_post();
$parentID = get_the_ID();
$childrenArgs = array(
'post_type' => 'page',
'post_parent' => $parentID ,
);
$children = get_children($childrenArgs);
foreach ($children as $child){
echo '<h1>' . $child -> post_title . '</h1>';
$content = $child -> post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
endwhile;
?>
I think you need to look into the action pre_get_posts. Something like this in your functions.php would do the trick.
function namespace_custom_query_vars( $query ) {
if ( !is_admin() && $query->is_main_query()) {
if ( $query->query["post_type"] == 'custom_post_type' ) {
$query->set( 'post_parent__not_in', 0 );
}
}
return $query;
}
add_action( 'pre_get_posts', 'namespace_custom_query_vars' );
There's a decent post about this here. Though note that the code on this page does not compile for small syntax errors.
What about using relations? A simple disjunctive union should do the charm.
$args = array(
'post_type' => POST_TYPE,
'posts_per_page' => 36,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => true
),
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => false,
'operator' => 'NOT IN'
)
)
);
Or is there a reason why not to consider this?

A Wordpress gallery for all types of media?

How can I display any type of media within a gallery in Wordpress?
Take a look at this question, its got extra comments. But I'll copy the important things to answer your question - https://wordpress.stackexchange.com/questions/11662/get-all-images-in-media-gallery
So this would get all attachments in the media library;
$media_query = new WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
)
);
$list = array();
foreach ($media_query->posts as $post) {
$list[] = wp_get_attachment_url($post->ID);
}
// do something with $list here;
Or just get all items of a specific type;
$query_images_args = array(
'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
}

Resources