Skip Navigation

[Resolved] Problem after migration of post relationships

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

Problem:

Use WP_Query to query related posts by new post type relationships.

Solution:

As the document, please try to use the relationship field slug instead of array, for example:

https://toolset.com/forums/topic/problem-after-migration-of-post-relationships/#post-1241340

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

This support ticket is created 5 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by Luo Yang 5 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1241297
After Migration.png
Before Migration.png

I had had the following code in my php template file.

<?php

$childargs = array(
'post_type' => 'attorney',
'numberposts' => -1,
'meta_key' => 'wpcf-last-name',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array(
'key' => '_wpcf_belongs_locations_id',
'value' => get_the_ID()))
);

$child_posts = get_posts($childargs);
foreach ($child_posts as $child_post) {
if(get_post_meta($child_post->ID, 'wpcf-office-contact', true) == '1') :
echo '

  • ';
    echo $child_post->post_title;
    if( get_post_meta($child_post->ID, 'wpcf-title') ):
    echo ' (' . get_post_meta($child_post->ID, 'wpcf-title', true) . ')';
    //echo '
    '.'
  • ';
    endif;
    echo '';
    endif;
    }

    ?>

    <?php

    $childargs = array(
    'post_type' => 'attorney',
    'numberposts' => -1,
    'meta_key' => 'wpcf-last-name',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(array(
    'key' => '_wpcf_belongs_locations_id',
    'value' => get_the_ID()))
    );
    $child_posts = get_posts($childargs);
    foreach ($child_posts as $child_post) {
    echo '

  • ';
    echo $child_post->post_title;
    if( get_post_meta($child_post->ID, 'wpcf-special-note') ):
    echo ' (' . get_post_meta($child_post->ID, 'wpcf-special-note', true) . ')';
    //echo '
    '.'
  • ';
    endif;
    echo '';
    }

    ?>

    Before migration to new post relationships, the pages looked like the attached ("Before Migration.png")

    After migration the page looks like the attached ("After Migration.png")

    I tried changing the code to this (below) (like i saw on this page https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/ ), but after doing so, only a partial list displayed.

    <?php

    $query = new WP_Query(
    array(
    'post_type' => 'attorney',
    'numberposts' => -1,
    //new toolset_relationships query argument
    'toolset_relationships' => array(
    'role' => 'child',
    'related_to' => get_the_ID(),
    // this will work only with relationships that have existed before the migration
    // if possible, use the relationship slug instead of an array
    'relationship' => array( 'locations', 'attorney' )
    ),
    'meta_key' => 'wpcf-last-name',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    )
    );
    $posts = $query->posts;
    foreach ($posts as $child_post) {
    echo '

  • ';
    echo $child_post->post_title;
    if( get_post_meta($child_post->ID, 'wpcf-special-note') ):
    echo ' (' . get_post_meta($child_post->ID, 'wpcf-special-note', true) . ')';
    //echo '
    '.'
  • ';
    endif;
    echo '';
    }

    ?>

    Can you let me know what wrong and how I can fix it?

    #1241340

    Hello,

    As the document mentioned above, please try to use the relationship field slug instead of array, for example, change this line from:

    'relationship' => array( 'locations', 'attorney' )
    

    To:

    'relationship' => 'location-attorney'
    

    You can get the post type relationship slug by this:
    Dashboard-> Toolset-> Relationships
    find and edit the post type relaitonship between post type "Location" and "Attorney", you will be able to see the relationship slug in input box: Slug (required)

    #1241674
    Before MIgration.png
    After change.png

    Even after changing it to:

    'relationship' => 'locations_attorney'

    I only get a partial list. Not all results display in the list. See attached comparison

    #1241683
    full list of relationship.png

    I looked at the location again to verify that all attorneys are listed in the relationship. They are, and there should be 11 in the list, but after changing the code to the following, only 5 are displayed. See attached.

    'relationship' => 'locations_attorney'
    #1241972

    I have tried above PHP codes in my localhost with a fresh wordpress installation + the latest version of Toolset plugins, it works fine, there isn't the similar problem as you mentioned above: get a partial list

    Please check these:
    1) Make sure you are using the latest version of Toolset plugins, you can download them here:
    https://toolset.com/account/downloads/

    2) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2019, deactivate all custom PHP/JS code snippets, and test again

    3) If the problem still persists, please provide database dump file(ZIP file) of your website, also point out the problem page URL, where and how do you put the custom PHP codes, I need to test and debug it in my localhost, thanks
    https://toolset.com/faq/provide-supporters-copy-site/

    #1242023

    Thanks for the details, I am downloading the files, will update here if there is anything found

    #1242025

    Here are what I found, in your PHP codes you are using below parameter:
    'numberposts' => -1,

    But there isn't such a a parameter "numberposts" within class WP_Query, you can replace it with:
    'posts_per_page' => -1

    And test again, see wordpress document:
    https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
    posts_per_page (int) - number of post to show per page

    #1242092

    Thanks Lou, That worked.

    I copied the code of 'numberposts' => -1 on this page:

    https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

    Maybe Toolset needs to update that page with the correct code from WordPress.

    Thanks

    #1243377

    Thanks for the feedback, I have forward the document issue to our editors.