• Resolved searay

    (@searay)


    Hello,

    I don’t know how to create a link from one post to another. Just to be clear, I know how to do simple link in HTML and even PHP, but my example seems too much for me.

    Link for the live website: link

    On the bottom left corner you’ll see a “Phoenix Pool & Spas” name on the blue background and I want this name linked to the “Phoenix Pool & Spas” post. For the moment it is linked to the “5474 Coach Ln” post, besically doing the same as “More details” button. I don’t want permanent link, cause it’s easy knowing the post ID, I want it to be dynamic. I hope it is possible.

    Here’s the piece of code for this link:

    <div class="detail">
                <h5 class="price">
                  <a href="<?php the_permalink(); ?>">
                  <?php
                  $post_meta_data = get_post_custom($post->ID);
    
                    if( !empty($post_meta_data['REAL_HOMES_property_contractor'][0]) ) {
                    $prop_contractor = $post_meta_data['REAL_HOMES_property_contractor'][0];
    
                    echo '<span>';
                    echo esc_html( $prop_contractor );
                    echo '</span>';
                   } 
    
                    ?></a>
                </h5>
                <p><?php framework_excerpt(12); ?></p>
                <a class="more-details" href="<?php the_permalink() ?>"><?php _e('More Details ','framework'); ?><i class="fa fa-caret-right"></i></a>
            </div>

    I hope to learn something here as well. Thanks for all the inputs.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Have a look at the Posts 2 Posts plugin. I also wrote a blog article some time ago about alternatives.

    Thread Starter searay

    (@searay)

    thanks jancbeck,

    not really sure how to use this plugin. I read the tutorial and did what it says, but it didn’t work for me.

    I put this part to my functions.php:

    <?php
    function my_connection_types() {
        p2p_register_connection_type( array(
            'name' => 'posts_to_posts',
            'from' => 'property',
            'to' => 'agent'
        ) );
    }
    add_action( 'p2p_init', 'my_connection_types' );
    ?>

    Checked the Connections Types in Tools and it shows 0…

    and this is how I modified my code to link from one post to the other:

    <div class="detail">
                <h5 class="price">
    <?php
    // Find connected pages
    $connected = new WP_Query( array(
      'connected_type' => 'posts_to_posts',
      'connected_items' => get_queried_object(),
      'nopaging' => true,
    ) );
    
    // Display connected pages
    if ( $connected->have_posts() ) :
    ?>
    <h3>Related pages:</h3>
    <ul>
    <?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php
                  $post_meta_data = get_post_custom($post->ID);
    
                    if( !empty($post_meta_data['REAL_HOMES_property_contractor'][0]) ) {
                    $prop_contractor = $post_meta_data['REAL_HOMES_property_contractor'][0];
    
                    echo '<span>';
                    echo esc_html( $prop_contractor );
                    echo '</span>';
                   } 
    
                    ?></a></li>
    <?php endwhile; ?>
    </ul>
    
    <?php
    // Prevent weirdness
    wp_reset_postdata();
    
    endif;
    ?>
    </h5>
                <p><?php framework_excerpt(12); ?></p>
                <a class="more-details" href="<?php the_permalink() ?>"><?php _e('More Details ','framework'); ?><i class="fa fa-caret-right"></i></a>
            </div>

    Any thoughts? Thanks!

    Thread Starter searay

    (@searay)

    Alright, I found out how to make a connection between two posts, but it seems the end results is not what I wanted. This plugin probably won’t help me.

    My questions is, how WordPress knows what is hidden behind this link:
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

    This code makes a link from the title of the post preview. What I want to achieve is to add another link in the preview that points to a different post types. Let’s say I have property address in the title of the post preview and when clicked it goes to the full property post(page), but at the same time I want to have a link(below the title of the same post) with a property agent name that when clicked will take me to the agent’s post(page).

    This is the current code for the property agent name showing as a link on the property preview, but when clicked it brings me to the property page instead agent’s page.

    <a href="<?php the_permalink(); ?>">
                  <?php
                  $post_meta_data = get_post_custom($post->ID);
    
                    if( !empty($post_meta_data['REAL_HOMES_property_contractor'][0]) ) {
                    $prop_contractor = $post_meta_data['REAL_HOMES_property_contractor'][0];
    
                    echo '<span>';
                    echo esc_html( $prop_contractor );
                    echo '</span>';
                   } 
    
                    ?></a>

    What should I put between <a> and </a> to make this work?

    Moderator bcworkz

    (@bcworkz)

    Your issue is with the_permalink(). It links to whatever post is in global $post. You want to use something like echo get_permalink( $id ); Where $id is the agent’s page ID. The question now is where can one get the ID? Anything in $post_meta_data?

    Thread Starter searay

    (@searay)

    This is the agent’s custom post type code:

    <?php
    /**
     * Create Custom Post Type : Agent
     */
    if ( ! function_exists( 'create_agent_post_type' ) ) {
    	function create_agent_post_type() {
    		$labels = array(
    			'name' => __( 'Builders', 'framework' ),
    			'singular_name' => __( 'Builder', 'framework' ),
    			'add_new' => __( 'Add New', 'framework' ),
    			'add_new_item' => __( 'Add New Builder', 'framework' ),
    			'edit_item' => __( 'Edit Builder', 'framework' ),
    			'new_item' => __( 'New Builder', 'framework' ),
    			'view_item' => __( 'View Builder', 'framework' ),
    			'search_items' => __( 'Search Builder', 'framework' ),
    			'not_found' => __( 'No Builder found', 'framework' ),
    			'not_found_in_trash' => __( 'No Builder found in Trash', 'framework' ),
    			'parent_item_colon' => ''
    		);
    
    		$args = array(
    			'labels' => $labels,
    			'public' => true,
    			'exclude_from_search' => true,
    			'publicly_queryable' => true,
    			'show_ui' => true,
    			'query_var' => true,
    			'capability_type' => 'post',
    			'hierarchical' => false,
    			'menu_icon' => 'dashicons-businessman',
    			'menu_position' => 5,
    			'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
    			'rewrite' => array(
    				'slug' => apply_filters( 'inspiry_agent_slug', __( 'agent', 'framework' ) )
    			)
    		);
    
    		register_post_type( 'agent', $args );
    	}
    
    	add_action( 'init', 'create_agent_post_type' );
    }
    
    if ( ! function_exists( 'inspiry_set_agent_slug' ) ) :
    	/**
    	 * This function set agent's url slug by hooking itself with related filter
    	 *
    	 * @param $existing_slug
    	 * @return mixed|void
    	 */
    	function inspiry_set_agent_slug( $existing_slug ) {
    		$new_slug = get_option( 'inspiry_agent_slug' );
    		if ( ! empty( $new_slug ) ) {
    			return $new_slug;
    		}
    		return $existing_slug;
    	}
    
    	add_filter( 'inspiry_agent_slug', 'inspiry_set_agent_slug' );
    endif;

    I think the agents names are stored in REAL_HOMES_agents.

    Thread Starter searay

    (@searay)

    I changed:

    if( !empty($post_meta_data['REAL_HOMES_property_contractor'][0]) ) {
                    $prop_contractor = $post_meta_data['REAL_HOMES_property_contractor'][0];

    to:

    if( !empty($post_meta_data['REAL_HOMES_agents'][0]) ) {
                    $prop_contractor = $post_meta_data['REAL_HOMES_agents'][0];

    and now instead of agent’s name it shows his ID, still linking to property though, but I think we’re close…

    If $prop_contractor contains your agents ID then you simply have to pass it to get_permalink in order to link to it (as @bcworkz mentioned before).

    <?php
    if( !empty($post_meta_data['REAL_HOMES_agents'][0]) ) {
          $prop_contractor = $post_meta_data['REAL_HOMES_agents'][0]
    }; ?>
    // linking the agents name
    <a href="<?php echo get_permalink($prop_contractor); ?>"><?php echo get_the_title(prop_contractor); ?></a>

    Template tags prefixed with “the_” often take the ID from the $post global and echo their value, while “get_” function can receive the ID as a parameter and return their value.

    Thread Starter searay

    (@searay)

    That’s very useful info @jancbeck, thanks. However, after I applied your code the link is still pointing to the property’s page and not the agent’s. Looks like the REAL_HOMES_agents doesn’t store his ID.
    I did some digging and found this code, which might store the agent’s ID, after some trials and errors I was able to pass the agent ID into the link and it’s linking to his page also, but the only problem now is that it always show the same agent on every property. image here
    Here is the code I found:

    // Agents
            $agents_array = array(-1 => __('None', 'framework'));
            $agents_posts = get_posts(array('post_type' => 'agent', 'posts_per_page' => -1, 'suppress_filters' => 0));
            if (!empty($agents_posts)) {
                foreach ($agents_posts as $agent_post) {
                    $agents_array[$agent_post->ID] = $agent_post->post_title;
                }
            }

    and the code I use now which returns the same value on every property instead the value that is assigned to every single property:

    <h5 class="price">
                 <?php  $agents_array = array(-1 => __('None', 'framework'));
            $agents_posts = get_posts(array('post_type' => 'agent', 'posts_per_page' => -1, 'suppress_filters' => 0));
            if (!empty($agents_posts)) {
                foreach ($agents_posts as $agent_post) {
                    $agents_array[$agent_post->ID] = $agent_post->post_title;
                }
            } ?>
                  <a href="<?php echo get_permalink($agent_post); ?>"><?php echo get_the_title($agent_post); ?></a>
    
                </h5>

    BTW: Many thanks for both of you @jancbeck and @bcworkz for all the help, I learned a lot here.

    Moderator bcworkz

    (@bcworkz)

    $agent_post just has the last agent’s post from the foreach loop, not the agent related to the current post. get_posts() is useful for getting all of the agent posts, but there is still nothing telling us which specific agent post to get.

    What you’ve found is useful because it tells us how to get agent posts, but we still don’t know which particular agent is related to the current post. We’re not going to get anywhere without this information. If it’s simply not available anywhere, something needs to be done so that it is available. An agent ID custom field for each post for example.

    Thread Starter searay

    (@searay)

    How can I do Agent’s custom ID field? I was thinking maybe by creating custom meta box in the admin? As I can see all the agents IDs, I could type them in manually, but this is slow process, are there any other options?

    Moderator bcworkz

    (@bcworkz)

    You can certainly add a custom meta box. There is something to be said for a dedicated meta box just for this item, but you could just use the Custom Fields box if you like. With custom fields it’s easier to forget to assign a value because nothing special is displayed when you add a new post. A custom meta box makes it more obvious some sort of data is needed. OTOH, no special coding is required to use custom fields, they are easy to use in that way.

    Even if there is no other recourse and you had to type in the IDs manually, you need to rely on something to determine what agent ID goes with any particular post. Where does this data reside or how is it determined which agent goes with which post? Whatever that is, there should be a way to code it to avoid manual entry. Even if it’s outside data, it could be imported somehow so a custom script can use it to populate the custom fields.

    Is there anything in the ‘REAL_HOMES_property_contractor’ post meta that also exists for the agent post type as well? Some sort of commonality between regular posts meta and agent posts meta should exist, I would think. If so, there should be a way to relate the two together. An agent ID would be useful, but any common data should work as long as it’s not ambiguous.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Creating link from one to post to the other.’ is closed to new replies.