0

I am creating a job board website(it is a part of the website). I have a JobOffer class with some rows in the database(added with it's form...). My problem now is to have a page for each of these JobOffers. I can't use it as a WP_Post because a want a custom rendering, i will need to add own data. This is my idea:

  • Create a page(from the wordpress dashboard -> Pages) with the name "Job Offer" and slug "job-offer"
  • Set the link a JobOffer like /job-offer?id={job_id}&label={job_label}
  • In the "Job Offer" page only add this shotcode: [single-job-offer]
  • In the shortcode definition get the id ($_GET['id']) and the label($_GET['label']) and render the content as i want
  • Change the page url and page title in the browser with javascript:
document.title = "The job title"
window.history.pushState("url_with_a_better_form");

But it is not a good idea.

Hoping i explain well what i want, what do you think i should do to follow SEO rules and have a page for all JobOffers, each of these pages having a title, a custom url and my shortcode as content.

EDIT: I followed the links given and i created my custom post type:

function phenix_custom_post_type()
{
    register_post_type(
        'phenix_job_offer',
        array(
            'labels'      => array(
                'name'          => 'Job Offers',
                'singular_name' => 'Job Offer',
            ),
            'public'      => true,
            'show_ui' => false,
            'rewrite' => array(
                'slug' => 'offres'
            )
        )
    );
}
add_action('init', 'phenix_custom_post_type');

And my template(single-phenix_blue_color.php):

<?php


get_header();
do_action('onepress_page_before_content'); // I'm working in my child theme and the parent is OnePress
?>

<div id="content" class="site-content">

    <?php onepress_breadcrumb(); ?>

    <div id="content-inside" class="container ">
        <div id="primary" class="content-area">
            <main id="main" class="site-main" role="main">

                <?php while (have_posts()) : the_post(); ?>

                    <?php get_template_part('template-parts/content', 'single'); ?>

                    <hr class="hr">
                    <?php
                    if (is_user_logged_in() && in_array('subscriber', _wp_get_current_user()->roles)) :
                        // 
                        if ($job_apps != null && count($job_apps) > 0) {
                            $tmp = array_filter($job_apps, function (JobApplication $elt) {
                                return is_int(strpos($_SERVER['REQUEST_URI'], $elt->job->post_name));
                            });

                            if (count($tmp) > 0)
                                echo "<div class='mt-2 mb-3 h6'><span class='alert alert-info'>" . do_shortcode('[icon name="circle-info" prefix="fas"]') . " ...</span></div>";
                        }
                    ?>

                        <p class="h5 mb-3">...</p>
                        <?= do_shortcode('[form-job-application]') ?>

                    <?php else : ?>

                        <p class="h6">
                            ////
                            <?php if (!is_user_logged_in()) : ?>
                                ///////
                            <?php endif; ?>
                        </p>

                    <?php endif; ?>

                <?php endwhile; ?>

            </main>
        </div>

    </div>
</div>

<?php get_footer(); ?>

Sorry for the length. I manually created a post in the database with the phenix_job_offer type and it displays well. I am still thinking on how i can more customize the template but my problem is: How can i tell the template to not try to fetch the post in the database but use my post(a custom wp_post that i will create just for the rendering of my JobOffer) ?

2 Answers 2

0

WordPress does not work this way. At least it's not recommended to cook WordPress this way because it uses different concepts you won't see in most other CMS and frameworks.

It'd be better to create a custom post type. In this case, your job offer postings permalinks will look like this: site.com/job-offfer/...

In this case, you'll be able to use all the features that WordPress provides for custom post types: meta fields, template structure, etc.

You can learn more about custom post types and how to handle them here: https://developer.wordpress.org/plugins/post-types/registering-custom-post-types/ https://developer.wordpress.org/plugins/post-types/working-with-custom-post-types/ https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/

2
  • So i have to create a new Post type like JobOffer and create a custom template for it. I'll try it, thanks. Feb 27, 2023 at 6:06
  • Please i still have a problem. I want to keep my JobOffers and just use the wordpress post_types to help me for the display. I don't want to create, edit or delete my JobOffers with the wordpress "program". I successfully create the post_type(thanks to your first link) but my work ends there. What should i do to render my JobOffer as if it was a custom_post without creating adding a custom post in database for all of my JobOffers ? Feb 27, 2023 at 8:46
0

I resolve the problem. What have i done ?

  • Create and register my custom post type, create a template for it (see my edit in the first message of the topic).
  • Create a small code that creates a post for each of my jobOffers with these fields:
// I also add this function in the `JobOffer` creation function so that the new ones will have a corresponding post
wp_insert_post([
   'post_content' => $jobOffer->description,
   'post_title' => $jobOffer->label,
   'post_excerpt' => substr($jobOffer->description, 75),
   'post_type' => 'phenix_job_offer',
   'post_name' => sanitize_title($jobOffer->label),
   'meta_input' => ['job_offer' => $jobOfferId]
]);
  • After that in my template i fetch the jobOffer with the post meta key ‘job_offer’ then render it as i want.
$post = get_post();

require_once('wp-content/themes/PhenixDOr-Plus/includes/repositories/repository-job-offer.php');
$jobOfferRepo = new JobOfferRepository();
$jobOffer = $jobOfferRepo->get(intval($post->job_offer));

// My template

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.