wordpress get_children doesn't reflect edits to image galleries - wordpress

I have a very simple theme that retrieves all image gallery links for each post using the get_children() function.
Unfortunately, there's some strange bugs - first, if images are not uploaded when creating the post and are instead selected from the media library, they don't show up after publishing. Also, if I do any edit after a post has been created, be it reordering the images, adding new ones, deleting images, even deleting the whole gallery and creating a new one, they don't show up either. Refreshing browser cache doesn't do a thing.
If i change to one of the base themes, the images show up, and the right links are there when querying the db directly.
I reproduced the basic problematic code in a one file micro-theme, but the issue still happens:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php echo $post->id;
$args = array(
'numberposts' => -1,
'order_by' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => inherit,
'post_type' => 'attachment'
);
$images = get_children($args);
if ( empty($images)){
echo "nothing";
}
foreach ( $images as $id => $image ) :
echo $image->post_title;
endforeach;
?>
<?php endwhile; ?>
<?php endif; ?>
I'm stumped, I've tried using different functions to retrieve the posts with the same result, deactivated all plugins, nothing.
I'm testing both on OSX mavericks and an Ubuntu vps, could it be some database cache thing i'm supposed to flush somewhere?

When you set 'post_parent' => $post->ID, you will only get attachments to that page. That's part of the reason not all of the images show up.
But the bigger problem is that galleries are handled differently than posts/pages/attachments. Galleries only exist in the shortcode- they're not stored in the db- so you can't query them with get_posts or get_children.
To customize the way galleries are rendered you'll have to either hook into the wordpress code that creates the gallery, or deregister that function and write your own.
This question answers how to manipulate shortcode if you were writing a plug-in- but the process will be the same from a theme.
:-)

Related

Remove images from content in wordpress gallery template

I want to add a gallery template to a child theme and am finding it frustratingly difficult to find good examples. The best example I've found to date is by Andres Hermosilla, but was made way back in 2012.
Steps 1 to 3 will get me half of what I want, which is a gallery inserted above the site title (I don't need fluxslider or similar at this stage.) The following is the code I'm using, placed just before the_title():
<?php
$image_args = array(
'numberposts' => -1, // Using -1 loads all posts
'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
'order'=> 'ASC',
'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
'post_status' => null,
'post_type' => 'attachment'
);
$images = get_children( $image_args );
if ( $images ) {
foreach ( $images as $image ) { ?>
<img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
<?php }
} ?>
My issue is that the images themselves remain displayed in the content of the post - how can I strip all of the images from the page content at the same time?
Why not add a class to the gallery template page and display none all the images except for gallery ones using css.

WordPress wp_query + filter result (no custom filed) + next_post_link()

I did an extensive search but could not find the answer (perhaps I'm not using the right search terms?).
Anyway, here's what I'm trying to accomplish:
I'm using wp_query to return user submitted posts that have been published. I am showing one post at a time and using next_posts_link() to let the user to advance to the next post. This is all working fine. I'm also using the wp-postratings plugin to the user to rate these user submissions. I only want to show posts that the user has not already rated. For that, I'm using:
check_rated_username($post->ID)
This part is actually also working. So far my code looks like this:
$my_query = new WP_Query( array (
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
));
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
$rated = check_rated_username($post->ID);
if (!$rated) : ?>
<?php //the_title() etc. Show post details (it's long so I'm not going to post the whole thing, but the post details are showing up fine) ?>
<?php next_posts_link('Next →', 10); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); endif; ?>
The problem next_posts_link() retrieves posts that meet the parameters set in the wp_query (and therefore are not part of the "if (!rated)" statement. There are instances when I click on "Next", and it shows a blank page--a post that this particular user has already rated.
How can I set it up so that I'm showing one post that the user has not rated on each page AND allow the user to navigate to the next unrated post by clicking on a NEXT button? I'm not married to the approach I have come up with. I just need to achieve this end result.
Thanks!
What you can do is first to fetch all posts id:s that a user has rate, then you can add an post__not_in in your query. It can look something like this:
<?php
/* Get all posts id:s that current user has rated */
$user_ratings = $wpdb->get_results( $wpdb->prepare( "SELECT rating_postid FROM {$wpdb->ratings} WHERE rating_userid = %d", get_current_user_id() ) ); ?>
<?php
/*Set up an empty array for save id:s in */
$post_not_in = array();
/* Check if $userRating has any values, if so loop thru and put the id into the array */
if($user_ratings){
foreach($user_ratings as $user_rating){
$post_not_in[] = $user_rating->rating_postid;
}
}
?>
<?php
/* The loop */
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'post__not_in' => $post_not_in
);
$my_query = new WP_Query($args);?>
The rest of your code
This way the loop only have posts that the current user haven't rated yet.

WordPress : Issue with wp_query and gd star rating plugin

I have used GD Star Rating plugin with my Wordpress installation, for creating 2 different sets of Ratings (Posts and Videos). (I was not able to find any other plugin that supports this feature).
I have used following arguments in one wp_query to display a list of top rated videos, and it's working fine.
$args=array(
'post_type' => 'youtube_videos',
'post_status' => 'publish',
'posts_per_page' => 10,
'gdsr_sort' => 'rating',
'gdsr_multi' => 3
);
On the same page, i am trying to show a list of top rated posts, with following arguments in wp_query, but it's not working.
$args=array(
'posts_per_page' => 4,
'post_type' => 'post',
'gdsr_sort' => 'rating',
'gdsr_multi' => 0
);
I found that issue is with value passes in 'gdsr_multi' varibale, if i remove this variable from the top rated videos 2nd part of code works fine.
I am not being able to run both together, any suggestions please?
For gdsr_multi variable documentation of GD Star rating plugin says:
For standard rating data, don't use this variable, or set it to zero.
To get multi ratings data this variable must be set to multi set id to use.
You should reset your query before start a new one. wp_reset_query(); will destroys the previous query used on a custom Loop. This function should be called after The Loop to ensure conditional tags work as expected. As example:
<?php
query_posts( 'post_parent=5' );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?><?php the_title() ?><br /><?php
endwhile;
endif;
wp_reset_query();
//your second query will be here.
?>
For more details, you can check Wordpress official codex: wp reset query

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.

Resources