• Just updated to 4.2.2 and a custom query that displayed sticky Custom posts (that was working great) just stopped working.

    I use Seamless Sticky Custom Posts plugin to extend the sticky post functionality to Custom Post Types.

    https://wordpress.org/plugins/seamless-sticky-custom-post-types/

    I also use Advanced Custom Fields to add metaboxes on the CPT edit screen so I can make it easy for someone to enter dates (using ACF datepicker) and also to keep the date format consistent (so someone doesn’t enter a date wrong and implode my code).

    I had a custom query setup to display the 4 most current CPTs that had the “stick to front page” option checked.

    Can someone tell me what has changed that would make Sticky Posts no longer work?

    This is my PREVIOUS code that was working fine before upgrading to 4.2.2 (from 4.1.1):

    <?php
    $dealsticky = get_option( 'sticky_posts' );
    $todaysdate = time('Ymd');
    $args = array(
                'post_type' => 'deal',
    	        'post__in'  => $dealsticky,
    	        'ignore_sticky_posts' => 1,
                'meta_key' => 'booking_date_end',
    			'meta_type' => 'DATE',
                'orderby' => 'meta_value',
                'order'   => 'ASC',
            	'posts_per_page' => 4,
                'meta_query' => array(
                    array(
                       'key' => 'booking_date_end',
                       'value' => $todaysdate,
                       'compare' => '>='
                       )
                       )
                       );
     $my_query = new WP_Query( $args );
     if ( $my_query->have_posts() ) { while ( $my_query->have_posts() ) : $my_query->the_post();
    $bookingstart = get_field('booking_date_start');
    $bookingend = get_field('booking_date_end');
    $travelstart = get_field('travel_date_start');
    $travelend = get_field('travel_date_end');
    $promocode = get_field('booking_promo_code');
    $available = get_field('available_to');
    $source = get_field('deal_source');
    $locations = get_field('locations');
    $offer = get_field('offer');
    foreach (get_the_terms( $post->ID , 'resort_category') as $term); {
    $even_odd = ( 'odd' != $even_odd ) ? 'odd' : 'even';
    ?>
    <li class="dealmodule bordered dropshadow clearfix <?php echo $even_odd; ?>">
    <div class="deal-title">
    <h6 class="entry-title"><?php the_title(); ?></h6>
    </div>
    <div class="deal-info">
    <div class="deal-thumb">
    	<?php if ( has_post_thumbnail() ) {
    	 the_post_thumbnail( 'featuredsm' );
    	 } else { ?>
        <img src="images/nophotoavailable.gif" width="150" height="86" alt="No Photo Available for this All Inclusive Resort" />
    <?php } ?>
    </div>
    <div class="deal-details">
    <span>Applicable Resort:</span>  <?php  the_field('applicable_resorts');  ?><br />
    <span>Locations: </span> <?php the_field('locations'); ?> <br />
    <span>The Offer: </span><?php the_field('offer'); ?><br />
    <span>Book between: </span>  <?php echo date('M d, Y', strtotime($bookingstart)); ?> - <?php echo date('M d, Y', strtotime($bookingend)); ?><br />
    <span>Travel between: </span>  <?php echo date('M d, Y', strtotime($travelstart)); ?> - <?php echo date('M d, Y', strtotime($travelend)); ?><br />
    <span>Available to: </span>  <?php echo $available; ?>
    </div>
    <div class="deal-source">
    <img src="<?php echo z_taxonomy_image_url($term->term_id); ?>" width="100" height="100" class="alignleft" />
    <span>Source:</span><br /><font style="color:red;"><?php echo $source; ?></font><br />
    <span>PROMO CODE:</span><br />
    <?php if (!($promocode)) {  echo "Not Required"; } else { echo $promocode; } ?><br />
    <a href="<?php the_field('deal_url'); ?>" title="<?php the_title(); ?>" target="_blank" class="btn"><i class="fa fa-hand-o-right"></i> Get This Deal </a>
    </div>
    </div>
    </li><?php } unset($term); endwhile; }?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The Plugin just implements the functionality to make a Custom Post ‘sticky’. It handles custom-post-stickies the same way like normal post stickies. Therefore it does not affect the query, but just put the custom-stickies in the options-list.

    I do not know, why this query worked before. The post__in filters the resultlist and accepts only posts contined in the list. Your query above filters the result list against the sticky-options-list. I would guess, that this query only finds your ‘deal’ stickies. See the post: Sticky Posts and Pagination again
    This should solve also your problem here.

    There are lots of problems caused by the wrong sticky-design in wordpress-core. I hope that it will be fixed within the core on one of the next releases, then plugins like the ‘Seamless Sticky Custom Post Types’ will be unnecessary.

    Another Tip: try to reinstall the plugin

    Thread Starter TrishaM

    (@trisham)

    @trimension,

    Thanks so much for looking at this…..I did try your suggested code yesterday with no luck, but I suspect it’s because I did not incorporate your code correctly into mine…..I have to admit that I’m not a coder, but I am good at following instructions (as long as they’re clear) and making sure I haven’t missed a closing brace or semicolon 🙂

    SO I’m not sure how to get in all of my necessary arguments as well as your function that modifies the post query. When I tried this, I get:

    Fatal error: Call to a member function have_posts() on a non-object in […]

    Basically in a nutshell this is what I’m trying to do, that worked prior to updating to 4.2.2:

    1. Query for posts of the Custom Post Type “Deal”
    2. Isolate only “Sticky” Deal Posts
    3. Check for the custom field (metadata) that is the deal’s expiration date, and don’t include any that have already expired (less than today’s date)
    4. Of those that are Sticky and have not expired, display the 4 most recent, in ASC order of expiration date (so that those that expire soonest are at the top)

    I’ve verified that we DO have more than 4 Unexpired Deals Posts that are sticky, but I don’t know how to get this working again – as a last resort, I may have to downgrade back to 4.1.1 – I do have a database backup from prior to upgrading.

    ——–
    Updated:

    I tried tweaking the query again, incorporating my args into the code referenced in the other thread, like so:

    function _modify_posts_query( $query ) {
    	if( $query->is_home() && $query->is_main_query()) {
    		$query->set('ignore_sticky_posts', true);
    		$query->set('post_type','deal');
    		$query->set('post__in', getSortedPostIds());
    		$query->set('meta_key', 'booking_date_end');
    		$query->set('meta_type', 'DATE');
    		$query->set('orderby', 'meta_value');
    		$query->set('order', 'ASC');
    		$query->set('meta_query', array(array('key' => 'booking_date_end','value' => $todaysdate,'compare' => '>=')));
    		$query->set('posts_per_page', 4);
    	}
    }
    add_action( 'pre_get_posts', '_modify_posts_query');
    
     if ( have_posts() ) { while (have_posts() ) : the_post();

    Seems like it *should* work but I get an error about my ‘foreach’ and it only returns the home page, not any of the sticky deals….the error message is:

    Warning: Invalid argument supplied for foreach() in […]

    The ‘foreach’ is the part of the loop that finds the parent term for the Deal so that the correct logo image can be displayed…the same line works perfectly on other pages that display all deals.

    Aaarrgh…..good thing I have a lot of hair, I’m tearing some of it out over this!

    Thread Starter TrishaM

    (@trisham)

    OK another update:

    I managed to clear the ‘foreach’ error – although the previous foreach code worked fine (because any child terms still referenced the same logo), I cleaned it up so it really does find the parent term….BUT…

    The loop still only returns (rather inexplicably) the Home Page (as a link) rather than the unexpired Sticky Deal posts. So it’s a list (of one item) and in the right format and style, but it’s the wrong Post.

    Here’s my current code:

    <div>
    <ul class="searchUL-deal clearfix">
    <?php
    	$todaysdate = time('Ymd');
    	function getSortedPostIds() {
    	global $wpdb;
    	$stickies = get_option('sticky_posts');
    	$sql = "SELECT ID, post_date, ID in(" . implode(',', $stickies) . ") as sticky FROM " . $wpdb->posts . " WHERE post_type = 'deal' ORDER BY sticky DESC, post_date DESC";
    	$posts = $wpdb->get_results($sql);
    	$pids = array();
    	foreach($posts as $p) {
    		$pids[] = $p->ID;
    	}
    	return $pids;
    	}
    function _modify_posts_query( $query ) {
    	if( $query->is_home() && $query->is_main_query()) {
    		$query->set('ignore_sticky_posts', true);
    		$query->set('post_type','deal');
    		$query->set('post__in', getSortedPostIds());
    		$query->set('meta_key', 'booking_date_end');
    		$query->set('meta_type', 'DATE');
    		$query->set('orderby', 'meta_value');
    		$query->set('order', 'ASC');
    		$query->set('meta_query', array(array('key' => 'booking_date_end','value' => $todaysdate,'compare' => '>=')));
    		$query->set('posts_per_page', 4);
    	}
    }
    add_action( 'pre_get_posts', '_modify_posts_query');
    
     if ( have_posts() ) { while (have_posts() ) : the_post();
     global $post;
    $bookingstart = get_field('booking_date_start');
    $bookingend = get_field('booking_date_end');
    $travelstart = get_field('travel_date_start');
    $travelend = get_field('travel_date_end');
    $promocode = get_field('booking_promo_code');
    // Get terms for post and find the parent term to display logo image associated with parent term
    $terms = get_the_terms( $post->id , 'resort_category');
    if($terms) {
        foreach( $terms as $term ) {
            $term = get_term_by('id', $term->parent, 'resort_category');
            if ($term->parent > 0) {
                $term = get_term_by('id', $term->parent, 'resort_category');
            }
            $cat_obj = get_term($term->term_id, 'resort_category');
            $cat_name = $cat_obj->name;
        }
    }?>
    
    [All my HTML is here that formats the results as list items, displays the logo and all the custom metadata associated with a deal, etc.]
    
    <?php unset($term); endwhile; } wp_reset_query(); ?>
    </ul>
    </div>

    Any suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP 4.2.2 Sticky (Custom) Posts stopped working’ is closed to new replies.