Skip Navigation

[Resolved] Archive Page displaying random post when no parent is associated.

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

Problem:
On a post archive or a view that lists posts of custom post type, such that:
-Posts Can (but do not have to be) children of the other post type.
-Posts with parents should have a link to the parent, posts without parents should not have such link.

The issue: if no parent is assigned, a random link to Parent CPT is displayed.

Solution:
Use wpv-if to evaluate if the CPT has a parent (Check the documentation).
If it returns true, the link will be displayed, on false, it won't.

Example:

-Problem:
--I need to check if each post int the loop has a parent post of type "Teams".Teams custom post slug is "teams"

-Solution:
--Use the "wpv-if" to check if the post has parent(s)
--Use the "_wpcf_belongs_{parent-slug}_id" to get the teams that the post belongs to them and assign it to a variable in the same condition like this (teams="_wpcf_belongs_teams_id").
--Check if the list of teams is empty using the "empty" function like this "empty($teams)".

[wpv-if teams="_wpcf_belongs_teams_id" condition="false" evaluate="empty($teams)"][wpv-post-link id="$teams"][/wpv-if]

Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#checking-post-parent

This support ticket is created 8 years, 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Author
Posts
#300420
screenshot.png

hidden link is an archive of the "rider" post type. Riders can (but do not have to be) children of the "team" post type. This archive view is set up as follows:

[wpv-layout-start]
	[wpv-items-found]
<h1> 2015 Bike Nantucket Participants</h1>
<br/>
	<!-- wpv-loop-start -->
	<wpv-loop>
		<div class="rider">[wpv-post-featured-image]
				<h4>[wpv-post-link]</h4>
          		<p>[wpv-post-link id="$teams"]</p>
			</div>
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]<strong>No posts found</strong>[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

On Riders that have a Parent "team" the team properly displays below their name and image. However, for riders that are not associated with a team, the archive puts a random team in that spot rather than leaving it blank.

For example, Jason Bridges and Joe Olson both show "Team Tuck" below their name, however they are not linked to that team at all (as confirmed by looking at edit screens for both their pages and Team Tuck.

How do I make it not display a team below the names of riders who are not linked to a team?

#300580

Thank you for contacting us here in the Support Forum

I think you should use a wpv-if conditional output here.
https://toolset.com/documentation/views-shortcodes/#wpv-if

You can see a example of wpv-if used to evaluate if a Parent Post type exists here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#checking-post-parent

When using Types posts relationships, Types stores the parent of a given type in a field

_wpcf_belongs_{parent-slug}_id. 

For your case, you could use something along this lines:
(assuming your parent (Team) slug is "teams")

[wpv-layout-start]
    [wpv-items-found]
<h1> 2015 Bike Nantucket Participants</h1>
<br/>
    <!-- wpv-loop-start -->
    <wpv-loop>
        <div class="rider">[wpv-post-featured-image]
                <h4>[wpv-post-link]</h4>

//Your wpv-if conditional (IF NOT EMPTY; Do something)
[wpv-if teams="_wpcf_belongs_teams_id" condition="false" evaluate="empty($teams)"][wpv-post-link id="$teams"][/wpv-if]
//End of your conditional output
             
            </div>
    </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        [wpml-string context="wpv-views"]<strong>No posts found</strong>[/wpml-string]
    [/wpv-no-items-found]
[wpv-layout-end]

Please let me know if you have further questions regarding the issue mentioned in this Thread
and let me know if the above solution works for you, I look forward to your reply!

Thank you

#300918

Works perfectly, thanks!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.