limit to parent and nested sub-level

Home Forums Product Support Forums Ajax Search Pro for WordPress Support limit to parent and nested sub-level

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 months, 1 week ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39786
    23volt
    23volt
    Participant

    We created an custom post type (contact-person) and this custom post type have option to set a parent post.
    For example

    Post ABC
    Parent:
    — A
    — A2
    ——- A3

    Now 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
    Tommy

    add_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  
    			);
    		}
    #39798
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39799
    23volt
    23volt
    Participant

    Hi friend!

    In our case the post have parent structure:
    -> parent 1
    –> sub-level 2
    —> sub-level 3

    So 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
    Tommy

    #39803
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    It’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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39808
    23volt
    23volt
    Participant

    So 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
    Tommy

    #39811
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    If those are the direct parents, then yes that should be it.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.