• I am using a child theme of DIVI themes. The parent theme returns the correct content but my child theme returns older content only. I’ve found the php file responsible for returning what was once working but hasn’t been working for about 3 months. My eyes are crossed trying to figure out what is wrong with this code, hoping someone here can help me with this.

    <?php get_header(); 
    //are we getting all sermon series parent posts or are we getting all subposts of a sermon series?
    $post_parent_qry = get_query_var( 'post_parent', 0 );
    if ($post_parent_qry == 0) {
    	$args = array('post_type' => 'message', 'post_parent' => (int) $post_parent_qry, 'posts_per_page'=>'20','order' => 'DESC');
    } else {
    	$args = array('post_type' => 'message', 'post_parent' => (int) $post_parent_qry, 'posts_per_page'=>'20','order' => 'ASC');
    }
    query_posts($args);
    ?>
    
    <div id="main-content">
    	<div class="container">
    		<div id="content-area" class="clearfix">
    			<div id="left-area">
    		<?php
    			//dcrosbie show parent post info
    				if ((int) $post_parent_qry > 0) {
    					echo "<h1>Sermon Series: " . get_the_title ((int) $post_parent_qry) . "</h1>";
    					echo get_the_post_thumbnail( (int) $post_parent_qry, 'full' );
    					echo "<p style='margin:30px'>" . get_post_field('post_content', (int) $post_parent_qry) . "</p>";
    					echo "<hr/>";
    				}
    				
    			if ( have_posts() ) :
    				while ( have_posts() ) : the_post();
    					$post_format = et_pb_post_format(); 
    					$post_parent_id = wp_get_post_parent_id( $post_ID );
    				
    					if ($post_parent_id == 0) {
    						//displaying sermon series parent posts
    						$the_post_link = get_site_url() . "/message/?post_parent=" . get_the_ID();
    						
    					} else {
    						//display subposts or a parent sermon series
    						$the_post_link = get_permalink();
    						
    					}
    					?>
    
    					<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>
    
    				<?php
    					$thumb = '';
    
    					$width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );
    
    					$height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
    					$classtext = 'et_pb_post_main_image';
    					$titletext = get_the_title();
    					$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
    					$thumb = $thumbnail["thumb"];
    
    					et_divi_post_format_content();
    
    					if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) {
    						if ( 'video' === $post_format && false !== ( $first_video = et_get_first_video() ) ) :
    							printf(
    								'<div class="et_main_video_container">
    									%1$s
    								</div>',
    								$first_video
    							);
    						elseif ( ! in_array( $post_format, array( 'gallery' ) ) && 'on' === et_get_option( 'divi_thumbnails_index', 'on' ) && '' !== $thumb ) : ?>
    							<a href="<?php echo $the_post_link ?>">
    								<?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
    							</a>
    					<?php
    						elseif ( 'gallery' === $post_format ) :
    							et_pb_gallery_images();
    						endif;
    					} ?>
    
    				<?php if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) : ?>
    					<?php if ( ! in_array( $post_format, array( 'link', 'audio' ) ) ) : ?>
    						<h2 class="entry-title"><a href="<?php echo $the_post_link  ?>"><?php the_title(); ?></a></h2>
    					<?php endif; ?>
    
    					<?php
    						et_divi_post_meta();
    
    						if ( $post_parent_id == 0 ) {
    							the_content();
    						} else {
    							truncate_post( 270 );
    						}
    					?>
    				<?php endif; ?>
    
    					</article> <!-- .et_pb_post -->
    			<?php
    					endwhile;
    
    					if ( function_exists( 'wp_pagenavi' ) )
    						wp_pagenavi();
    					else
    						get_template_part( 'includes/navigation', 'index' );
    				else :
    					get_template_part( 'includes/no-results', 'index' );
    				endif;
    			?>
    			</div> <!-- #left-area -->
    
    			<?php  dynamic_sidebar('sermon-sidebar'); ?>
    		</div> <!-- #content-area -->
    	</div> <!-- .container -->
    </div> <!-- #main-content -->
    
    <?php get_footer(); ?>

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If you don’t specify a “paged” arg, with ASC order you’ll get the oldest posts. If you supply a “paged” arg, you’ll get ASC order of whatever page was requested. This has always been the case, not sure how it could have previously worked otherwise.

    What might be at play is the use of query_posts(). It’s not the preferred way to get posts even though it’s still supported. Mainly because when used to replace the main query, an extra, unused time consuming query is made. It’s recommended that main queries be altered through the “pre_get_posts” action where you can set or unset query vars as desired before any query is made, avoiding a wasteful second query.

    By altering the main query instead of making another one, the requested paged var will already be set, but for ASC order you’ll need to subtract the requested page number from the max pages + 1 value. For example, with ASC order, getting page 1 returns the very oldest posts. Getting the last page gets the newest posts.

    Thread Starter speedysweedy

    (@speedysweedy)

    Interesting, ok so I get what you’re saying but I have no idea how I should implement the changes you suggested. Would you be able to help me with that?

    Moderator bcworkz

    (@bcworkz)

    I know you said PHP is over your head, but you’re going to need to give some cut and paste coding a go. Find examples that best fit your need, then carefully edit as needed to fit your situation. Errors will be inevitable. I’m willing to help you resolve errors, but I’m not going to develop all the code for you. If that’s what you want, I can suggest where you can hire professional help.

    There are some examples on the pre_get_posts doc page. Use $query->set() to set desired query var values. To unset undesired values that were set by WP, set the query var to either 0 or an empty string as appropriate. Place your coding attempt in functions.php of your child theme. Comment out the query_posts() call on your template.

    The important thing is to only target queries you want to target since all queries go through this action. ! is_admin() && $query->is_main_query() are usually the right things to check as a start, but you’ll need to be more specific than that.

    Try var_dumping the main query’s vars with var_dump( $query->query_vars ); to see what vars are normally set for the pages you want to target. You can use these to further qualify the correct requests. The usual HTML view of var_dump is almost unreadable, but if you look at the page’s HTML source it’ll be much more structured. Or var_dump into a <pre>...</pre> block.

    It occurs to me that to get the most recent posts sorted ASC, you’d need to know how many posts in all are returned. This cannot be known until after the query is made. I think you’d need to query sort by default DESC first, then on your template reverse the returned array of posts to get ASC order.

    TBH, you can still use query_posts() and forget about doing it the right way ?? Remove the ASC query arg and instead, after calling query_posts() conditionally do this to reverse the array:

    global $wp_query;
    $wp_query->posts = array_reverse( $wp_query->posts );
    Thread Starter speedysweedy

    (@speedysweedy)

    Oh absolutely, I would never expect you to code the whole thing for me but just having your help is amazing and I’m very thankful for that. I’ll start digging into it, I’m sure I’ll get it with your help.

    Thanks again!

    BTW, I have the code working on my site now, I simply changed the ASC line to DESC and the DESC line to be ASC so I’ve bought myself some learning time.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PHP Over my head’ is closed to new replies.