Community Forums › Forums › Archived Forums › Design Tips and Tricks › Get index of custom query in the action hook?
- This topic has 12 replies, 3 voices, and was last updated 7 years ago by Brad Dalton.
-
AuthorPosts
-
March 30, 2017 at 5:44 am #204045trashParticipant
Hello,
I do a custom query and mix it with genesis hooks in order to get the same markup:$myposts = get_posts($args); echo '<h2 class="grid-home-headline"><span>'.esc_html($headline_one).'</span></h2>'; foreach ( $myposts as $post ) : setup_postdata( $post ); echo '<div class="fp-article-fw-wrapper">'; echo '<div class="fp-article-fw-image two-thirds first">'; the_post_thumbnail(); echo '</div>'; echo '<div class="fp-article-fw-text one-third">'; do_action( 'genesis_entry_header' ); do_action( 'genesis_before_entry_content' ); printf( '<div %s>', genesis_attr( 'entry-content' ) ); do_action( 'genesis_entry_content' ); echo '</div>'; do_action( 'genesis_after_entry_content' ); do_action( 'genesis_entry_footer' ); echo '</div>'; echo '</div>';
How can I get the index of $myposts inside the action hook?
I tried it with:add_action('genesis_entry_header', 'custom_genesis_entry_header', 8); function custom_genesis_entry_header() { global $myposts; echo $myposts->current_post; }
But this is not working. Does anybody know that?
Thanks!
March 30, 2017 at 7:21 am #204051Victor FontModeratorI suggest installing the Kint debugger so you can view the content of $myposts. https://wordpress.org/plugins/kint-php-debugger/
Once you install the plugin, put a d($myposts); right after global $myposts;
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 30, 2017 at 7:28 am #204053trashParticipantHey Victor,
it says -> NULL. So somehow the variable is not known tot the hook.$myposts NULL
?’Called from <ROOT>project/wp-content/themes/customtheme/front-page.php:49 [custom_genesis_entry_header()]
<ROOT>project/wp-includes/class-wp-hook.php:298 [call_user_func_array()]
<ROOT>project/wp-includes/class-wp-hook.php:323 [WP_Hook->apply_filters()]
<ROOT>project/wp-includes/plugin.php:453 [WP_Hook->do_action()]
<ROOT>project/wp-content/themes/customtheme/functions.php:1498 [do_action()]
<ROOT>project/wp-includes/class-wp-hook.php:298 [call_user_func_array()]
<ROOT>project/wp-includes/class-wp-hook.php:323 [WP_Hook->apply_filters()]
<ROOT>project/wp-includes/plugin.php:453 [WP_Hook->do_action()]
<ROOT>project/wp-content/themes/customtheme/front-page.php:61 [do_action()]
<ROOT>project/wp-includes/template-loader.php:74
<ROOT>project/wp-blog-header.php:19
<ROOT>project/index.php:17March 30, 2017 at 11:35 am #204065Victor FontModeratorIt means it's not a global variable. You have to look at the way you're declaring $myposts and when. It could be a timing issue. You may be trying to access the variable before it's declared.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 30, 2017 at 1:17 pm #204066Brad DaltonParticipant@trash What are you trying to do?
I think it would be more effecient to use genesis_custom_loop with your own custom $args
March 30, 2017 at 3:18 pm #204074trashParticipantBasically I have one front page which has 4 parts and is fed through several custom post fields that I have created with ACF. The user in the backend can select 4 different categories for those 4 parts which are displayed on the front page. The query gets the latest posts for each category that has been selected.
I could solve this by creating 4 WP_Query's to select the posts of the 4 selected categories, create a foreach loop and my own HTML structure to get it going. But I was asking myself if I also can create 4 different genesis_custom_loops with the data from the custom fields, but then I have no possibility to target those 4 different loops with hooks, because the structure of the posts should be different.
So, when I use a hook like genesis_entry_content, I can only ask if (is_front_page()), but not something like is_front_page && custom query number 4.
So I came up with using the genesis hooks, but not genesis_custom_loop and there I do not have the possibility to get the index of my custom $myposts variable in order to determine which HTML structure I want to show.
Is this explained clearly or confusing? 🙂
March 30, 2017 at 3:26 pm #204075Brad DaltonParticipantSome of it is clear to me. genesis_custom_loop uses WP_Query parameters which as you know, enables you to control everything within each loop.
Hooks are already included.
If you want to control the markup for each loop, you could use WP_Query rather than genesis_custom_loop.
March 31, 2017 at 12:42 am #204094trashParticipantYep, that's what I thought as well. Using WP_Query and putting the genesis_action_hooks in the loop as above. How would your solution look like? Same as above?
March 31, 2017 at 12:51 am #204095Brad DaltonParticipantI would look in the page_team.php file in the Infinity Pro child theme as that removes and adds hooks as well as using genesis_custom_loop so it may help you.
Hope that helps.
March 31, 2017 at 2:06 am #204099trashParticipantThanks for the advise. I see what it is done there, but in my case it does not solve my issue. So I think I still have to copy the genesis hooks in my own set of functions.
I tried it with replicating their code as follows, but then both genesis_custom_loops are showing the same posts. I do not know why, as $section_one and $section_two are having different category ids:
remove_action('genesis_loop', 'genesis_do_loop'); add_action( 'genesis_loop', 'infinity_team_loop' ); function infinity_team_loop() { global $post; global $wp_query; $section_one = get_post_meta( get_the_ID(), 'kategorien_1', true ); $headline_one = get_post_meta( get_the_ID(), 'headline_1', true ); $args = array( 'posts_per_page' => 4, 'offset' => 0, 'category' => $section_one, 'category_name' => '', 'orderby' => 'date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'post_mime_type' => '', 'post_parent' => '', 'author' => '', 'author_name' => '', 'post_status' => 'publish', 'suppress_filters' => true ); $loop = new WP_Query( $args ); // Setup the team entry actions. add_filter( 'post_class' , 'infinity_team_class' ); genesis_custom_loop( $args ); remove_filter( 'post_class' , 'infinity_team_class' ); wp_reset_postdata(); wp_reset_query(); $section_two = get_post_meta( get_the_ID(), 'kategorien_2', true ); $headline_two = get_post_meta( get_the_ID(), 'headline_2', true ); if (!empty($section_two)) { $args2 = array( 'posts_per_page' => 5, 'offset' => 0, 'category' => $section_two, 'category_name' => '', 'orderby' => 'date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'post_mime_type' => '', 'post_parent' => '', 'author' => '', 'author_name' => '', 'post_status' => 'publish', 'suppress_filters' => true ); } $loop = new WP_Query( $args2 ); // Setup the team entry actions. add_filter( 'post_class' , 'infinity_team2_class' ); genesis_custom_loop( $args2 ); remove_filter( 'post_class' , 'infinity_team2_class' ); wp_reset_postdata(); wp_reset_query(); }
Do you know why?
March 31, 2017 at 6:02 am #204111Victor FontModeratorThere is no wp_query parameter called category. WP_Query category parameters are:
- cat (int) - use category id.
- category_name (string) - use category slug.
- category__and (array) - use category id.
- category__in (array) - use category id.
- category__not_in (array) - use category id.
You should be using "cat".
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 31, 2017 at 3:05 pm #204133Brad DaltonParticipantUntested but i don't think you need:
global $post; Because you're using get_the_ID() with custom fields wp_reset_query(); Because you already use wp_reset_postdata();
add_filter( 'post_class' , 'infinity_team_class' ); Any reason you need to filter the post class? Normally done to add column classes.
This is how you normally use custom fields in a WP_Query
$query = new WP_Query( array( 'meta_key' => 'kategorien_1' ) );
<script src="https://gist.github.com/braddalton/620c0163d7fcac24454595ffe2baaa38.js"></script>
As well as what Victor suggested, i think those things need changing.
Its only custom field data right?
March 31, 2017 at 3:11 pm #204134Brad DaltonParticipantDoesn't look like you can embed gists anymore.
Hope that helps otherwise i would look for a tutorial or theme which provides working code you can modify.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.