• Resolved Gemfruit

    (@gemfruit)


    From all of my research, I’ve seen that it’s apparently not possible to have a child page belong to multiple parents, which I’m fine with.

    I do however, need a workaround. I need to have the functionality to mark a page as belonging to both “Restaurants”, and “Bars”. Each has a separate parent page. If there was a way to mark the page as both, I could then query it to be listed in the directories for both, even if the final page only resided at one url, belonging to the more dominant of the two (or more) parents.

    I began with categories, which allow for this functionality, but switched to pages, as they gave me greater functionality, and the content being listed is static (businesses in an area, not articles for instance).

    Any ideas on how to achieve this?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Have you considered using custom fields?

    Thread Starter Gemfruit

    (@gemfruit)

    I hadn’t, as I wasn’t actually aware of their existence. Upon reading a bit, I’m amazed I never knew about them, as they seem extremely powerful.

    Given my situation, it seems I want something like this:

    <?php $categories = get_post_meta($post->ID, 'categories', false); ?>

    False will return every value for the meta key of “categories” (may want to change the name, just to stay away from the default WordPress Page / Post / Category), which I can then check to see if the category I care about in that specific query is there. For instance, if I’m on the restaurants page, and a restaurant / bar page has the custom field values of “bar” and “restaurant” in the categories meta, I can then display it on the “Restaurant” page because I know it’s also a restaurant, and not just a bar.

    I would have to retrieve the array and store it in the $categories variable, then loop through that returned array to check for whatever the current page name was. I’m not exactly sure how to write that code (php syntax is still rough for me), so if anyone has any bumps in the right direction, that would be great!

    (may want to change the name, just to stay away from the default WordPress Page / Post / Category

    Yes – I think that really would be a very good idea. Try something like $whatisthis in place of $categories and change the name of the custom field from categories to something like sector. In general, the more exotic the name the better if you want to avoid weird results due to variable name conflicts.

    How about something like:

    <?php
    $whatisthis = get_post_meta($post->ID, 'sector', false);
    if( in_array( 'restaurant', $whatisthis ) ) :
    [do Restaurant stuff]
    else:
    [do Bar stuff]
    endif;
    ?>
    Thread Starter Gemfruit

    (@gemfruit)

    in_array, what a handy function haha. I should really spend some time looking through all of the pre-made functions for WordPress and php. I’m a decent programming, I just only edit php, rarely write my own.

    As for your code, that looks like it should do the trick. I’ll be applying / merging that with a pre_get_posts hook and a custom page template I’m using (I’m checking for the directory custom page template, then in my pre_get_posts determining how to display the query). Between what you posted, and what I have, I should be able to get it working, thanks!

    I’ll mark this post as resolved myself if I get it, otherwise I may be back with another question or two. Thanks again, hopefully I’ve got what I need.

    Glad I could help ??

    Thread Starter Gemfruit

    (@gemfruit)

    I came across a small issue.

    Since this code is in functions.php as a pre_get_posts (or even if it was on my parent page template), I need to revise it a bit. As is, I would run the above code on the loop of children. The problem is, I need the proper list of children before I can run the loop.

    The only solution I can think of, is to loop through all of my pages (or at least pages that have children), use the above check, then give myself a modified loop to work with after. The reason for this, is once I have my proper list to loop through, I then need to grab the Yoast SEO meta description, and the featured image of that page.

    Side note, I can’t seem to figure out how to grab just the children. I’m trying to use get_page_children, but that doesn’t seem to be working (I think I’m lost on the parameters of the function).

    Does anyone have any insight on how I should go about gathering which pages to run the check on? Am I fine to check all pages? (Will be about 100-200 pages). It’s frustrating knowing the logic, but not the code ??

    Thread Starter Gemfruit

    (@gemfruit)

    So I’ve worked on this some more, and I’m really close to having it exactly as I want it.

    I got the custom fields working, and I can pull in any page that shares the name with the directory page (dynamically), which is awesome. I got the Yoast SEO meta description to show, and the featured image, sweet. The only thing I haven’t been able to get, is the styling of this information, as it was before.

    I’m essentially translating this:

    <?php if (have_posts()) : ?>
    						<?php while (have_posts()) : the_post(); ?>
    
    						<div <?php post_class("full-width-post") ?> id="post-<?php the_ID(); ?>">
                                <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(260,200), array("class" => "alignleft post_thumbnail")); } ?>
    							<h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    							<div class="entry">
    								<?php echo(get_post_meta($post->ID, '_yoast_wpseo_metadesc', true)); ?>
    							</div>
    						</div><!--/post-<?php the_ID(); ?>-->
    
    				<?php endwhile; ?>
    
    				<?php else : ?>

    Into what I have now:

    <?php 
    
    		$this_page_title = get_the_title();
    
    		$args = array(
    			'sort_order' => 'ASC',
    			'sort_column' => 'post_title',
    			'meta_key' => 'directory_category',
    			'meta_value' => $this_page_title,
    			'post_type' => 'page',
    			'post_status' => 'publish'
    		);
    
    		$pages = get_posts($args); 
    
    		if($pages) {
    
    			foreach ($pages as $page) { ?>
    
    				<div class="full-width-post" >
    					<?php echo(get_the_post_thumbnail($page->ID, array(260,200))); ?>
    					<h2 class="title" id="post-<?php $page->ID; ?>"><a href="<?php echo(get_permalink($page->ID)); ?>" rel="bookmark" title="<?php echo(get_the_title($page->ID)); ?>"><?php echo(get_the_title($page->ID)); ?></a></h2>
    
    					<div class="entry">
    					<?php echo(get_post_meta($page->ID, '_yoast_wpseo_metadesc', true)); ?>
    					</div>
    				</div>
    			<?php
    			}
    		}
    	?>

    The issue seems to be where I replaced post_class with a generic div tag with the same class, that didn’t translate to well (or the class isn’t even being used, I can’t tell).

    What this should look like, as it did before, is the featured image on the left, the title on the top next to the image, with the meta description just below that to the right of the image. I’m amazed the styling is the part I’m stuck on, after learning all the wordperss php calls I just did haha. Anyone have any ideas how to get that class working, or if it is working, why it doesn’t look right?

    Thread Starter Gemfruit

    (@gemfruit)

    I had left out the second array to align-left the post thumbnail woops.

    Issue resolved, thanks for all of the help that lead me to figuring everything out!

    I realize that this is an old post and the OP has found a solution, but in the event that future visitors stumble on this, the best solution available is Scribu’s fantastic Posts-to-Posts plugin. It’s a developer-oriented plugin that allows for creating complex many-to-many relationships in WordPress, which is not functionality that is available in core.

    https://github.com/scribu/wp-posts-to-posts

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Pages With Multiple Parents’ is closed to new replies.