Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › limit to parent and nested sub-level
- This topic has 5 replies, 2 voices, and was last updated 3 years, 7 months ago by
Ernest Marcinko.
-
AuthorPosts
-
October 26, 2022 at 5:42 pm #39786
23volt
ParticipantWe created an custom post type (contact-person) and this custom post type have option to set a parent post.
For examplePost ABC
Parent:
— A
— A2
——- A3Now i wanna show all posts which have parent A and all nested parents (A2, A3).
In other words: how to show posts from specific parent with all nested sub-parents?
I created this code with your help, but its only working wih first level parents.
For example its working for this:
post ABC have parent A.
But if post have parent A -> A2 … its not working.Maybe you can help me to fix that sitiation please?
BR
Tommyadd_filter('asp_query_cpt', 'asp_change_query_directly', 10, 1); function asp_change_query_directly($q) { global $wpdb; $q = preg_replace('/AND ' . preg_quote("$wpdb->posts.post_parent IN ") . '\((.*?)\)/im', "OR ($wpdb->posts.post_type IN ('post', 'page', 'dealer', 'vehicle', 'contact-person') AND $wpdb->posts.post_status LIKE 'publish' AND $wpdb->posts.post_parent IN($1))", $q); return $q; } add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 ); function asp_include_only_parent_ids( $args, $id ) { if ( $id == 3 ) { $args['post_parent'] = array(11748); $args['post_meta_filter'][] = array( 'key' => 'ad_location', 'value' => array('123', '456'), 'operator' => 'ELIKE', 'allow_missing' => false ); }October 27, 2022 at 1:47 pm #39798Ernest Marcinko
KeymasterHi,
This is only possible if the sub-parent page is selected (in this case A2). Parent-child hierarchy in wordpress is only stored in one level, so the “grandchild” post is not marked with the original grandparent ID.
October 27, 2022 at 1:53 pm #3979923volt
ParticipantHi friend!
In our case the post have parent structure:
-> parent 1
–> sub-level 2
—> sub-level 3So the post this value in parent selector: Sub-level 3. The main-parent of sub-level 3 is parent 1.
With my code we query all posts with parent 1 and their sub-levels. But its not working. We see onyl posts with parent 1, but no posts with sub-level 2 or sub-level 2.Why its not working?
BR
TommyOctober 27, 2022 at 2:32 pm #39803Ernest Marcinko
KeymasterIt’s becasue Parent 1 -> sub-level 3 have nothing in common. Posts in “sub-level 3” have no direct connection to “Parent 1”. Unfortunately this is how wordpress works in this case. To exclude posts from “sub-level 3” the direct parent “sub-level 2” has to be added.
October 27, 2022 at 3:30 pm #3980823volt
ParticipantSo if i wanna include also sub-parent 2 and sub-parent 3 i need to add their post id also.
Is this code correct:
if ( $id == 3 ) {
$args[‘post_parent’] = array(11748, 24547, 8787);I seperate the post id of each parent/level per comma. Thats right?
BR
TommyOctober 27, 2022 at 3:41 pm #39811Ernest Marcinko
KeymasterIf those are the direct parents, then yes that should be it.
-
AuthorPosts
- You must be logged in to reply to this topic.