Querying Stickies in Custom Query Shortcode
-
I’ve adapted the idea in this tutorial (https://digwp.com/2010/01/custom-query-shortcode/) to create a custom query shortcode.
I’ve got it all working, listing each entry in alphabetical order by surname with a shortcode such as:
[staffloop the_query="post_type=staff&order=ASC&meta_key=surname&orderby=meta_value&departments=crt&posts_per_page=-1"]
The adapted function behind that is:
function staffloop_shortcode($atts) { // EXAMPLE USAGE: // [loop the_query="showposts=100&post_type=page&post_parent=453"] // Defaults extract(shortcode_atts(array( "the_query" => '' ), $atts)); // de-funkify query $the_query = preg_replace('~�*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query); $the_query = preg_replace('~�*([0-9]+);~e', 'chr(\\1)', $the_query); // query is made query_posts($the_query); // Reset and setup variables $output = ''; $temp_title = ''; $temp_link = ''; // the loop if (have_posts()) : while (have_posts()) : the_post(); $temp_title = get_the_title($post->ID); $temp_link = get_permalink($post->ID); $first_name = get_post_custom($post_id, 'first_name', true); $surname = get_post_custom($post_id, 'surname', true); $title_eng = get_post_custom($post_id, 'title_eng', true); $title_de = get_post_custom($post_id, 'title_de', true); $email = get_post_custom($post_id, 'email', true); // output all findings - CUSTOMIZE TO YOUR LIKING if(qtrans_getLanguage()=='en'): $output .= "<strong>" . $first_name['first_name'][0] . "?" . $surname['surname'][0] . "</strong><br /> <span class='title'>" . $title_eng['title_eng'][0] . "</span> <div class='clearfix'></div> <a href='mailto:" . $email['email'][0] ."'>Email</a><div class='clearfix' style='border-bottom:1px dotted #CCC;margin-bottom:10px;padding-bottom:10px;'></div>"; endif; if(qtrans_getLanguage()=='de'): $output .= "<strong>" . $first_name['first_name'][0] . "?" . $surname['surname'][0] . "</strong><br /> <span class='title'>" . $title_de['title_de'][0] . "</span> <div class='clearfix'></div> <a href='mailto:" . $email['email'][0] ."'>E-Mail</a><div class='clearfix' style='border-bottom:1px dotted #CCC;margin-bottom:10px;padding-bottom:10px;'></div>"; endif; endwhile; else: $output .= "nothing found."; endif; wp_reset_query(); return $output; } add_shortcode("staffloop", "staffloop_shortcode");
However, where this fails is in terms of sticky posts. They get ignored and appear as part of the regular alphabetical order where I would really like to push them to the top. I tried passing a post__in parameter to the query such that it ended up looking like:
[staffloop the_query="post_type=staff&order=ASC&meta_key=surname&orderby=meta_value&departments=crt&posts_per_page=-1&post__in=get_option( 'sticky_posts' );"]
to no avail. In case it was something wrong with the get_option bit as it related to the preg_replace lines in the function, I tried to declare a global variable in my functions.php:
$sticky = get_option( 'sticky_posts' );
And subsequently adjusted the shortcode to:
[staffloop the_query="post_type=staff&order=ASC&meta_key=surname&orderby=meta_value&departments=crt&posts_per_page=-1&post__in=$sticky"]
Also to no avail.
At this point, I’m at a loss and am reaching out here for advice from people better versed in this stuff than I.
Many thanks!!!
- The topic ‘Querying Stickies in Custom Query Shortcode’ is closed to new replies.