Skip Navigation

[Resolved] Modify the WP Query with a Custom Field Post ID

This thread is resolved. Here is a description of the problem and solution.

Problem:
Modify the WP Query with a Custom Field Post ID - how to display nested shrotcode view with elementor

Solution:

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/modify-the-wp-query-with-a-custom-field-post-id/page/2/#post-2037605

Relevant Documentation:

This support ticket is created 3 years, 9 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 19 replies, has 2 voices.

Last updated by kristofG 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2035161

At the bottom of a CPT "Case", the client needs to hand pick 3 related items from different categories, 1 is another web page and 2 other CPT. As we have an Elementor Template for "Case", we need to make this selection of the 3 related items dynamic, so we created 3 custom fields for Case (related-expertise, related-case, related-knowhow).

Elementor lets us use custom queries (hidden link) but we are struggling to load the wpcf-related-expertise we entered for a test case.

add_action( 'elementor/query/related-expertise', function( $query ) {
$args = array(
'key' => 'wpcf-related-expertise',
);
$query = WP_Query($args);
} );

#2035385

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The thing is that you shared the Elementor hook and I honestly do not have any exposer with Elementor hook. I suggest better you should contact the Elementor support and let us know if they require any help from Toolset.

#2035451
Screenshot_3.png
Screenshot_2.png
Screenshot_1.png

I understand.
What if we'd use Toolset View for our Query. How would we be able to select a post/page based on the ID that is filled in in a custom field?

#2035487

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should add a custom field Query filter as you already did and use the option filter by shortcode attribute.

More info:
=> https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

Does that helps?

#2035491
Screenshot_4.png

I was already heading in that direction before your last post, but still struggling.
I have hardcode the ID for testing, "2" is a sample page
[wpv-view name="related-expertise" related="2"]
but it still says no items found

#2035525

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is strange.

Can you please share problem URL where you added view with shortcode attribute as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2036535

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me where exactly you added the following view:
=> hidden link

#2036537

Looks like you found it 🙂 as I see you are currently editing the Single Case Elementor template.

#2036539

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - I see it but I do not able to locate where you added the view on Elementor template?
=> hidden link

#2036541

sorry for the confusion, I did not realize the shortcode had again been replaced by the TP Dynamic Listing widget.
I have corrected it, it is in the left column at the bottom.

#2036565

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now:
=> hidden link

The issue was you have set your view to query wrong post type (pages). I've set the view to query correct post type "Case" and I can see its working as expected.

#2036567

Well, client requested to load a specifc Page in the left column, a specific Case (CPT) in the middle column and a specific Know How (CPT still to be built) in the right column.
Is that possible?

#2036573

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - but for that you need to build view for your each post type and add it to place where you desire.

#2036607

I know, but it doesn't seem to work.

I have now 2 Views, 1 queries the Pages (Related Expertise), the other queries the CPT Cases (Related Cases).
In order to simply test if the short code works, I have hard coded 2 IDs, Page #583 and Case #350.
Result: the page query shows no results found and the case query shows the current case instead of #350.

I have now changed my 2 Views to filter 'Include only posts with IDs set by the View shortcode attribute "related"' instead of 'Select items with field: Related Expertise is a string equal to VIEW_PARAM(related)' And this now shows the requested Page and Case.

However, we cannot of course hard code the ID in a template, we need to dynamically "read" the input from the Custom Field.

So how do we replace "583" in [wpv-view name="related-expertise" related="583"] with the content of the Custom Fields "related-expertise" and "related-case"?

#2036609

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If you want to filter your view by dynamic value, you can pass the Types shortcode:

For example:

[wpv-view name="related-expertise" related="[types field='related-expertise'][/types]"]