Skip Navigation

[Résolu] Search by parent and child posts and display parent and child posts counter

This support ticket is created Il y a 6 années et 3 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Ce sujet contient 24 réponses, a 3 voix.

Dernière mise à jour par vimalS Il y a 6 années et 2 mois.

Assisté par: Nigel.

Auteur
Publications
#1089198
Screenshot_1.png
Screenshot_2.png

Tell us what you are trying to do?
-- I have custom post type called services. In that I have parent post(service provider) , and child post (services).
Now, I would like to implement search in a view where they can do search by typing parent post(service provider) , and child post (services) title.
-- I have to display counter of parent post(service provider) and child post (services)
-- Search should work using ajax and counter of parent post(service provider) and child post (services) will be updated based on search results.

Is there any documentation that you are following?
-- I have created view for the same.
-- I have created 2 shortcodes to display counter of parent post(service provider) and child post (services) and added it to toolset. But when I do search the counter are not getting updated and when I search using parent post name it is not showing me its related child post name..

Is there a similar example that we can see?

What is the link to your site? : lien caché

Additionally, I have installed relevanssi search plugin.

Please check attached screenshot for better understanding.

#1089487

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Sorry, I'm having problems understanding what you are aiming for.

You have a search View with text search and custom field (or taxonomy?) filter.

What post type does this View display? Services? Or service providers?

Looking at your card results I'm guessing that they are services (which display the service title and the parent service provider title).

If that's the case then you can only filter or search by service fields (standard fields or custom fields).

So people wouldn't be able to search for the service provider name.

But I may have misunderstood.

So could you please clarify what the View settings are, and if you have created custom shortcodes to output post counts I'll need to see those. Their not updating may simply be a question of updating the View results with a page refresh rather than by ajax.

Note that a View can output the number of matching results using the wpv-found-count shortcode (https://toolset.com/documentation/user-guides/views-shortcodes/#vf-155378).

#1090169

Looking at your card results I'm guessing that they are services (which display the service title and the parent service provider title).

-- Yes, Correct

If that's the case then you can only filter or search by service fields (standard fields or custom fields).

So people wouldn't be able to search for the service provider name.

But I may have misunderstood.

So could you please clarify what the View settings are, and if you have created custom shortcodes to output post counts I'll need to see those. Their not updating may simply be a question of updating the View results with a page refresh rather than by ajax

-- Yes, I have created custom shortcode to display parent and child counter but It is not getting updated using ajax search.

#1090202

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Where have you added the shortcodes?

#1091140
Screenshot_2.png
Screenshot_1.png

Check attached screenshot for the same.

#1091196

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

shortcodes-with-ajax.gif

I prepared a simple test with this shortcode:

add_shortcode( 'microtime', function(){

	return microtime();
});

It just outputs the current UNIX time in milliseconds.

I then inserted it in the filter controls section of my View, like so:

[wpv-filter-start hide="false"]
[wpv-filter-controls]
[microtime]
<div class="form-group">
	<label>[wpml-string context="wpv-views"]Categories[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="category" type="select" url_param="wpv-category"]
</div>
[/wpv-filter-controls]
[wpv-filter-end]

You can see the results in the screen recording. Whenever I update the filter, the timestamp updates.

From which I can conclude that the shortcodes are parsed again with each ajax update when the filters are changed.

You might want to add the same shortcode on your View to confirm the same.

So the problem would seem to lie in the code of the shortcodes themselves.

Can you share the code?

#1091214

Yes, Please check my code

add_shortcode('parent_service_count','codex_parent_service_count');
function codex_parent_service_count(){
    $args = array(    	
        'post_type' => 'service',        
        'post_parent__in' => array(0),
        'posts_per_page' => -1
    );
    $query = new WP_Query( $args );
    $html ='';
    $i = 0;
    if ( $query->have_posts() ) :

        while ( $query->have_posts() ) : $query->the_post();
        	$i++;
         endwhile;
        wp_reset_postdata();
    endif;    
    return $i; 
}

add_shortcode('child_service_count','codex_child_service_count');
function codex_child_service_count(){
    $args = array(    	
        'post_type' => 'service',        
        'post_parent__not_in' => array(0),
        'posts_per_page' => -1
    );
    $query = new WP_Query( $args );
    $html ='';
    $i = 0;
    if ( $query->have_posts() ) :

        while ( $query->have_posts() ) : $query->the_post();
        	$i++;
         endwhile;
        wp_reset_postdata();
    endif;    
    return $i; 
}

Thanks!!

#1091269

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

A couple of observations.

First you can simplify getting the number of results like so:

	$query = new WP_Query( $args );

	$i = count( $query->posts );

But the main issue is that your query takes no account of the filters that have been applied, so why should the results be any different each time the shortcode is run?

You can use the wpv-found-count shortcode as I described above and it will show the number of matching service posts updated according to whatever filters you apply.

And as this View is only querying services (not service providers), the number of service providers will always be the same.

So I'm not sure why you need the custom shortcodes (or, at least, why you need to change them, you can just replace the custom shortcode for the number of service results with wpv-found-count).

#1093708
Screenshot_1.png

I can not use [wpv-found-count]

The post type services itself having service providers as parent post.
So, I need count for both and it is not possible without custom shortcode.
see screenshot

#1093742

Hi,

This is to notify you that Nigel is off today. he will continue working on your ticket once he gets back tomorrow.

Thanks.

#1094759

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

vitae.gif

Ah! Thanks for the screenshot.

I had understood you were using post relationships to connect a Service Provider post type with a Services post type, but I see that there is only a single services post type which is hierarchical, and you want to display counts of the top-level posts and second-level posts.

The problem remains as before, though, "that your query takes no account of the filters that have been applied, so why should the results be any different each time the shortcode is run?"

You can examine the $_POST object to see what is available to identify the search term which you can then use in your shortcode to update the queries used to retrieve the counts.

I set up a test on a local site to try this and you can see the results in the screen recording. The shortcodes I used are registered like so:

add_shortcode( 'count-parent', function(){

	$post_type = 'service';
	$search = '';

	if ( isset( $_POST['search']['dps_general'][0]['value'] ) ) {

		$search = $_POST['search']['dps_general'][0]['value'];
	}

	$args = array(
		'post_type'			=> $post_type,
		'posts_per_page'	=> -1,
		'post_parent'		=> 0,
		's'					=> $search
	);
	$query = new WP_Query($args);

	$i = count($query->posts);

	return $i;

});

add_shortcode( 'count-child', function(){

	$post_type = 'service';
	$search = '';

	if ( isset( $_POST['search']['dps_general'][0]['value'] ) ) {

		$search = $_POST['search']['dps_general'][0]['value'];
	}

	$args = array(
		'post_type'				=> $post_type,
		'posts_per_page'		=> -1,
		'post_parent__not_in'	=> array(0),
		's'						=> $search
	);
	$query = new WP_Query($args);

	$i = count($query->posts);

	return $i;

});
#1094839

Hi Nigel,

The shortcodes you gave above are working perfectly fine.
But the toolset serach box is not searching data properly..for some characters it is showing results and for others it is not showing.. I have used relevanssi as well with toolset search

URL : lien caché

If you type : "online" it is showing results and updating parent child post counts..
if you type : "por" it is updating count but not showing results..

Any idea why?

Thanks

#1095014

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

I'm not sure about that.

Can you disable where you added the shortcodes and disable Relevanssi, and see if that affects this.

If you are using Relevanssi are the indexes up to date?

#1095630

Can you disable where you added the shortcodes and disable Relevanssi, and see if that affects this.

-- Disabled relevanssi and its working

If you are using Relevanssi are the indexes up to date?

-- Indexes are upto date.

Thanks

#1095684

Also, counters are not getting updated when I apply filter..