Forum Replies Created
-
AuthorPosts
-
June 18, 2014 at 10:26 am in reply to: Shortcode with custom loop in page affecting search results #110371
jspada
MemberRyan,
Thank you for your help. Your code is much cleaner than mine was and Im starting to get my head around the proper way to accomplish what I was going for.
Links to your gists were in the email notification I received about your reply. I guess the email was sent before they were deleted.
I replaced my code with yours and it is just about there, Im thinking. Just a few things.
The first is still a big one. The shortcode is still not showing its query results on the the search results page. You can see that here, Memoriam test search results, its the first result on the page. This is the major issue. If you click the link and go to the actual page Im using the short code on you can see that it does work there on the individual page.
Second, if you scroll down the search results page a bit you will notice that other results, ones that are not of type memoriam, have the 'FILED UNDER: CATEGORY' printed twice. I can fix that by removing
genesis_post_meta();
from the jspt_memoriam_post_meta function. However, in addition to that, the posts of type memoriam, still have 'FILED UNDER:' in the footer although just without the actual category. The jspt_memoriam_post_meta function you created just seems to remove the metadata and not the entire entry footer area.I also want to remove the entry content for memoriam post types. So can I rewrite the jspt_memoriam_post_meta function like this?
add_action( 'genesis_entry_footer', 'jspt_memoriam_change_entry_output' ); /** * Callback for Genesis 'genesis_entry_footer' action. * * if the current post type is 'memoriam', do not display the entry footer or entry content. * * @return */ function jspt_memoriam_change_entry_output() { if( 'memoriam' === get_post_type() ) remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); return; // call the default entry meta //genesis_post_meta(); }
This doesnt really work, I dont think genesis_entry_footer is that right action to hook into. What would your suggestion be on for the correct hook? I tried genesis_loop and genesis_before_loop but hooking in there causes no output at all to be displayed. Are those actions too early to hook into?
Thanks again for your help, it has been very insightful.
June 16, 2014 at 3:34 pm in reply to: Shortcode with custom loop in page affecting search results #110044jspada
MemberHello,
Thanks for your advice. I dont really understand why I cant put filters/hooks inside short codes. It seems that if I want to perform an action that is already applied to a fitler or hook, then why would I write the code again to do the same thing? But I will deffiantly try it.
Why I want the shortcode to do this is because I need to be able to output these lists of posts, on a post or page, any place I want them. With text above, below, or in between the shortcodes. Thats why I didnt try to create a template, it may not be the same useage every time I want to output the CPT on a post or page. It also needs to display the custom meta of the CPT and not the standard meta of a post type. I also need to sort based on my custom meta for the CPT.
As you can see on the links to the example pages I entered in my original post above, on the page, it outputs as expected, some text, the output of the list with one argument, then some more text, the output of the shortcode with another argument, then some more text at the bottom. works great.
However, on the search results page, the page with the shortcode in it is at the top of the list. I altered my original code above, see below, to now be just a copy of the custom loop and then the standard loop directly from the loops.php file from genesis. I removed a couple of the filters that I didnt want to perform in the loop and it still works as expected on the page containing the shortcode. now in the search results it does not affect the rest of the results, but in the results, the page still does not display any posts from the shortcode. Im getting 'Sorry, no content matched your criteria.'. So it seems like the loop is being run, but its not pulling any posts with the query arguments when its display as a search result. So here is the code Im currently using but I will also try building it without the use of any filters or hooks and just code in the desired markup and output. Though it really doesnt make sense to me why I should be able to do this.
// Filter for In Memoriam entry meta post info to include Department and Deceased Date function jspt_memoriam_post_info_filter($post_info) { if ('memoriam' == get_post_type()) { $post_info = '<span class="entry-department">'.get_post_meta( get_the_ID(), 'wpcf-department', true).'</span> Deceased: <time class="entry-time" itemprop="dateDeceased" datetime="'.get_post_meta( get_the_ID(), 'wpcf-deceased-date', true).'">'.date("F j, Y", get_post_meta( get_the_ID(), 'wpcf-deceased-date', true)).'</time> [post_comments] [post_edit]'; return $post_info; } } // In Memoriam Shortcode Handler function jspt_memoriam_shortcode_handler( $atts ) { // Retrive attributes extract( shortcode_atts( array('ffld' => false), $atts ) ); // If no attribute provided notify if ( $ffld == false ) { // Notify error of no attribute in shortcode return 'You must specify ffld="yes" or ffld="no" with in the shortcode.'; // If the $ffld attribute is set } else { // Perpare post query arguments $args = array ( 'post_status' => 'publish', 'post_type' => 'memoriam', 'posts_per_page' => -1, // Unlimited posts per page 'meta_key' => 'wpcf-deceased-date', 'orderby' => 'meta_value_num', // Order by Deceased Date 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'wpcf-firefighter-line-of-duty', // Firefighters Line of Duty only 'value' => $ffld, // yes or no 'compare' => 'IN' ) ) ); // Filter the Post info to include Department and Deceased Date add_filter( 'genesis_post_info', 'jspt_memoriam_post_info_filter' ); ob_start(); // Run the loop global $wp_query, $more; $defaults = array(); //* For forward compatibility $args = apply_filters( 'genesis_custom_loop_args', wp_parse_args( $args, $defaults ), $args, $defaults ); $wp_query = new WP_Query( $args ); //* Only set $more to 0 if we're on an archive $more = is_singular() ? $more : 0; if ( ! genesis_html5() ) { genesis_legacy_loop(); return; } if ( have_posts() ) : while ( have_posts() ) : the_post(); do_action( 'genesis_before_entry' ); printf( '<article %s>', genesis_attr( 'entry' ) ); do_action( 'genesis_entry_header' ); echo '</article>'; do_action( 'genesis_after_entry' ); endwhile; //* end of one post do_action( 'genesis_after_endwhile' ); else : //* if no posts exist do_action( 'genesis_loop_else' ); endif; //* end loop // Empty the buffer into a variable $html = ob_get_clean(); //* Restore original query wp_reset_query(); // Return output return $html; } // End if } // End Shortcode Handler // Add Shortcode function my_init() { add_shortcode( 'jssd-in-memoriam', 'jspt_memoriam_shortcode_handler' ); } add_action('init' , 'my_init'); function jspt_memoriam_single_page() { if ( is_singular( 'memoriam' ) || is_search('memoriam') ) { // Filter the Post info to include Department and Deceased Date add_filter( 'genesis_post_info', 'jspt_memoriam_post_info_filter' ); // Remove the post Meta remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); } } add_action( 'genesis_before_loop', 'jspt_memoriam_single_page' );
Thank you again for any help you can provide.
I will try to write a method of doing this without any filters as you suggested and let you know how it turns out.
-
AuthorPosts