plugins – Want to build parent-child relationships between custom post types created with CPT UI

I’ve gone through similar queries from the past but couldn’t find a solution that works for me.

So I created 3 different custom post types with the CPT UI plugin. They are called ‘careers’, ‘vacancies’, ‘our-staff’, respectively.

What I want to achieve is set ‘careers’ as the parent, and both ‘vacancies’ and ‘our-staff’ as children of ‘careers’, so that the breadcrumb lists on the children pages dynamically show as follows:

Home > Careers > Vacancies

Home > Careers > Our Staff

In the CPT UI interface, hierarchical is set to ‘true’ for all 3 custom post types.

For the two intended children pages ‘vacancies’ and ‘our staff’, ‘Parent’ is set as ‘careers’.

However the breadcrumb lists on the intended children pages still show as

Home > Vacancies

Home > Our Staff

i.e. skipping the parent page.

When I try getting the parent page ID of the children pages, it shows as “0” meaning the parent page is not set as intended.

I’ve tried using the Add-Hierarchy-Parent-to-Post plugin, but no luck….

Would be grateful for any tips, including workarounds to get the result I am hoping for.

filters – ACF Relationship Previous Next links of CPT

I have a CPT for artworks with custom taxonomy for artists. Then I have another CPT for artists. The artist pages are basically the the page title (artist name) and a relationship field where you add the artist’s artwork from the artworks CPT where you can filter by the custom taxonomy (since there are 50+ artists and 100s of artworks).

Each artwork is its own page as mentioned. When you click on the artwork from an artist page, we want to have the option of going to the next or previous artwork based on the order displayed on the artist page as determined by the order you set from its relationship field.

I’ve tried many solutions but none have worked yet. The only way I even get a hint of success is by adding a ACF field to the artworks post type that selects the artist post type via a post object field. I was using THIS PAGE as my guide.

There seems to be an issue with the following code. I’m note sure how $current_post_id applies to the array_search. If I print_r $current_index nothing appears. Same goes for $prev_module and $next_module. However, if I print_r $module_ids the array appears as it should in the order I set on the Artist page in the Relationship field. $first_module and $last_module show the correct IDs too. I just can’t seem to get the current post ID to then determine next and previous.

// create empty array for module ids
$module_ids = array();
             
// loop through modules and add them to array
foreach( $modules as $module ) :
    $module_ids() = $module->ID;
endforeach;
             
// get the current index
$current_index = array_search( $current_post_id, $module_ids );
             
// find the prev/next items
$prev_module = $current_index - 1;
$next_module = $current_index + 1;
             
// find first and last modules
$first_module = $module_ids(0);
$last_module = end($module_ids);

custom post types – CPT EVENT – listing by month and filtering by category and month – form select option

I am creating custom post type eventlist filter form. There is two options: category and months.
Following this advice List events by month I got display event categories by month. Meaning…

January

  • Event post 1
  • Event post 2

February

  • Event post 1
  • Event post 2

etc.

Category choosing works smoothly. Problem comes up when user choose month. By default events are listed by month. How can I reorder events using form options? Is it possible solve using meta_query compare or do I need some other solution?

<form method="get" action="<?php the_permalink();?>" name="tapahtumalista">
<select class="form-select" name="kat">

<?php 
$terms = get_terms( array(
    'taxonomy'      => 'kategori_tapahtumat',
));


// SELECT OPTION CATEGORY
?>

    <option value="">Valitse kategoria</option>
    <?php 
    
    foreach ($terms as $term) { ?>
        <option value="<?php echo $term->slug; ?>" <?php if(isset($_GET('kat')) && $_GET('kat') == $term->slug) { echo 'selected="selected"'; }?>>
        <?php echo $term->name; ?></option>
    <?php } ?>
</select>

<?php  // SELECT OPTION MONTH ?>
<select class="form-select" name="kk">
    <option value="">Valitse kuukausi</option>
    <?php 
    for ($i = 1; $i <= 12; $i++){
    $month_name = date_i18n('F', mktime(0, 0, 0, $i, 1, 2011)); 
    $month = !empty( $_GET('kk') ) ? $_GET('kk') : 0;
    $selected = $month_name == $month;
    ?>
    <option value="<?php echo $month_name; ?>" 
    <?php if(isset($_GET('kk')) && !empty($_GET('kk')) == $selected) { echo 'selected="selected"'; }?>
    <?php $selected ?>> 
        <?php echo $month_name; ?>
    </option>
    
    <?php }?>
</select>
<input type="submit" name="" value="Valitse">
// Selected category from form option 
if($_GET('kat') && !empty($_GET('kat'))) {
    $selected_cat = $_GET('kat');
}

//Selected month from form option
if($_GET('kk') && !empty($_GET('kk'))) {
    $selected_month = $_GET('kk');  
}


$today = date('Ymd');
$m = get_the_time('m');

$args =  (
    'post_type'         => 'tapahtumat',
    'post_status'       => 'publish',
    'posts_per_page'    => - 1,
    'meta_key'          => 'aloituspaiva', //event start date
    'meta_value'        => $today,
    'orderby'           => 'meta_value',
    'value'             => $today,
    'meta_compare'      => '>=',
    'type'              => 'DATE',
    'order'             => 'ASC',
    'tax_query'         => (
        (
            'taxonomy'  => 'kategori_tapahtumat',
            'field'     => 'slug',
            'terms'     => $selected_cat, //category get from form 
        )
    ),
    'meta_query'        => (
        'relation'      => 'OR',
        (
            
            'month'     => $m,
            'value'     => $selected_month, // get from form possible?
            'compare'   => '=',
        )

    )
);
$query = new WP_Query($args);

// List events by month (default view) - using dboris solution

$all_events = ();

if($query->have_posts()) :
    while ($query->have_posts() ) : $query->the_post();
        $date = strtotime(get_post_meta( get_the_ID(), 'aloituspaiva', true ) );
        $month_year = date( "F", $date );
        $all_events( $month_year )() = $query->post;    
    endwhile;
    // Sorting events by month

    foreach ( $all_events as $month_year => $events) : ?>
        <?php echo '<br><br><strong>' . $month_year . '</strong><br>';  ?>

        <?php foreach ( $events as $event ) : ?>
            <?php echo $event->post_title . '<br>';?>
        <?php endforeach;?>
    <?php endforeach;
else :
    echo ('No results.');
endif;

wp_reset_postdata(); 

wp query – Display related CPT with custom taxonomy

I have two custom post types: “storefront_location” and “services”

These are linked by a custom taxonomy “location”

I am building a post template for “storefront_location” and would like to display a listing of the “services” posts associated with any given storefront.

What I’m trying to achieve:

A “storefront-location” post, London, is categorized with “london” (“location” taxonomy).
I would like that London “storefront-location” post to display all “services” which are also categorized with “london” (“location” taxonomy). Users could then click on each service to read about it, since these are post types.

I’m trying to get the taxonomy information of the current post (London) and use tax-query to find all the services that share the same taxonomy with that specific London post.

Note: this is for a WP_query filter in Elementor, so the extra code for the Elementor query is from their tutorial here.

I’m fairly new to this, so there might be a newbie problem here. I’ve been trying to figure this out from other code examples here on stack exchange. I’d appreciate any help!

add_action( 'elementor/query/my_filter', function( $query ) {
//get the storefront-location post's location
   $custom_terms = wp_get_post_terms($post->ID, 'location');

if( $custom_terms ){

    //hold tax query parameters
    $tax_query = array();


    // loop through locations and build a tax query
    foreach( $custom_terms as $custom_term ) {

        $tax_query() = array(
            'taxonomy' => 'location',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        );

    }

    // put all the WP_Query args together
    $args = array( 'post_type' => 'services',
                    'posts_per_page' => -1,
                    'tax_query' => $tax_query );

    // finally run the query
    $loop = new WP_Query($args);

    if( $loop->have_posts() ) {

        while( $loop->have_posts() ) : $loop->the_post(); ?>

        <div class="listing-title"><?php the_title(); ?></div>     
        <?php endwhile;
    }

    wp_reset_query();

}
});

plugins – Add Custom Search Box for CPT in Admin area

I want to add a custom search box in edit.php . but i did not find the filter for doing that.

as you see in the picture, I want to add another search form next to the default WordPress one.
The request must be in Ajax: I must reload the result array instantly

enter image description here

rewrite tag – How to build a achieve page for a custom CPT with multiple taxonomy button filter

This is an extended question from
Permalink for CPT with taxonomy


I am building a website that has a custom CPT and a few custom taxonomies.
The goal is to setup the archive page to have a front-end filter with buttons.

I have struggling with a few different questions…hoping someone can help.

Here is a screenshot of the HTML mark for the achieve page:

filter html markup screenshot

Here’s the current setup:

Custom Post Type:
English Speaking, with slug name “english-speakings”

Custom Taxonomies:
(1) Question Tasks (“english_speaking_tasks”), and its terms: “task1”, “task2”, “task3”

(2) Difficulties (“english_speaking_difficulties”) and its terms: “easy”, “advanced”, “pro”

(3) Course (“english_speaking_courses”) and its terms: “course-a”, “course-b”, “course-c”

Custom CPT: English-Speakings

register_post_type( 'english-speakings', array(
    'label' => 'English Speaking',
    'public' => true,
    'rewrite' => array(
        'slug' => 'english/speaking/%speaking_task%/%course%/',
    ),
    // Other args here.
) );

Taxonomy

// Task
register_taxonomy( 'speaking-task', array( 'english-speaking' ), array(
    'label' => 'Speaking Tasks',
    'public' => true,
    'rewrite' => array(
        'slug' => 'english/speakings',
    ),
    // Other args here.
) );


// Difficulties 
register_taxonomy( 'english_speaking_difficulties', array( 'english-speaking' ), array(
    'label' => 'Speaking Tasks',
    'public' => true,
    'rewrite' => array(
        'slug' => 'english/speaking/difficulties',
    ),
    // Other args here.
) );



// Course
register_taxonomy( 'english_speaking_courses', array( 'english-speaking' ), array(
    'label' => 'Speaking Tasks',
    'public' => true,
    'rewrite' => array(
        'slug' => 'english/speaking/courses',
    ),
    // Other args here.
) );

'slug' => 'english/speaking/%speaking_task%/???????/',

**Question: For the URL format, I need some guidance or suggestion here… **

For SEO purpose, I like to setup a Clean URL, but I do not need all the taxonomy names to show up there, and I might have more taxonomies created in the future…

Instead of https://example.com/{cpt-slug}/{task-slug}/{course-slug}/{difficulty-slug}
( https://example.com/english/task1/course-a/pro )

The ideal format is:

Example: https://example.com/english/task1/2035 or 20-35 or 20/35
( where 20 and 35 are the term id: course-a(“20”), pro(“35”))


Register the custom var with WP.

I have three taxonomy that need to be functional as filters, so I am going to register them to WP as custom vars:

function cpt_english_register_query_vars( $vars ) {
    $vars() = 'course';
    $vars() = 'task';
    $vars() = 'difficulty';
    return $vars;
}
add_filter( 'query_vars', 'cpt_english_register_query_vars' );

Build Query for the CPT Achieve page

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; //use 'page' if the query is on a static front page
    
    
    $course =  get_query_var( 'course' ) ?: "courseA"; //set default value as "courseA"
    $task = get_query_var('task') ?: "task1"; // default value "task1" 
    $difficulty = get_query_var('difficulty') ?: "easy"; //default value "easy"
    
    $args = array( 
        'post_type' => 'english_speakings',
        'post_status'    => 'publish',
        'posts_per_page' =>  20 ,
        'paged' => $paged,
    
         'tax_query' => array(
    
           'relation' => 'AND', 
            
            array(
                'taxonomy' => 'english_speaking_tasks',
                'field'    => 'slug',
                'terms'    => $task,
            ),
    
            array(
                'taxonomy' => 'english_speaking_course',
                'field'    => 'slug',
                'terms'    => $course,
            ),
    
            array(
                'taxonomy' => 'english_speaking_difficlties',
                'field'    => 'slug',
                'terms'    => $difficulty,
            ),
    
        ),
    );

$the_query = new WP_Query( $args );

And Output the custom Query:

<?php

if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) {

        $the_query->the_post();
?>

Content ...

<?php
    }
    wp_reset_postdata(); //wp_reset_query();
} else {
    echo '<div class="card-body"><p>Sorry, there are no posts to display</p></div>';
}
?>

And I am lost….

The following are the parts I have trouble with:

(1) I have trouble making up my mind about the URL structure, need advise here

(2) “Adding Rewrite Tags”.. I do not understand how to use rewrite tags, regex and match1, match(2) ..these are very confusing to me

(3) For the front-end the taxonomy term buttons, I am not sure how to build the for each filter term buttons.

(4) I am not sure how to linked the custom var with the correct taxonomy term

Deleting CPT post by code (ie in a plugin) redirects to the main post type

  1. Suppose a CPT
  2. In functions.php or in a plugin, hook on ‘save_post’ to perform some
    calculations upon hitting the Publish button in WP-Admin…
  3. Inside the hook’s code include some conditional logic that under
    certain circumstances it will delete the CPT post that is just being
    created. Ie study the following piece of code:
add_action('save_post', 'import_external_data', 10, 2);
function import_external_data($post_id, $post)
{
    if ($post->post_status == 'publish' && $post->post_type == 'movie') {
        // Check if movie exists
        $movies = get_posts(array(
            'numberposts' => 1,
            'post_status' => 'publish',
            'post_type' => 'movie',
            'meta_key' => 'movie_id',
            'meta_value' => $post->post_content,
        ))(0);

        if (empty($movies)) {
            // Various actions take place here
        } else {
            wp_delete_post($post_id, true);
        }
    }
}
  1. Once the post is deleted you are redirected to the stock post type
    (?post_type=<CPT_slug> part of the URL is gone)

I already reported this in WordPress’s bug tracker, but I wonder if it’s really a bug or if I’m doing something wrong…

metabox – Create custom Content Editor for CPT

Hi I’m new to WordPress theme dev.

I’m trying ro create a custom theme (child theme) in order to create a CPT for books. There I would like to have 3 separate WYSIWYG editors (english, french, german) at the same time.

Is using custom meta boxes the right approach for custom content?

If not, what would bei recommended?

How to Edit CPT Content in Local Editor

Can you please let me know if it is possible to locally connect to a Custom Post Type . let’s say Movies and a Post for example "Avatar" and edit it local editors like VS Code and Post it back to WP?

Technically instead of using WP editor use my own local CODING editor

plugins – Change upload folder for a CPT

I have a CPT called “Musterkuechen”. Users have access to a custom dashboard that I created with ACF Frontend. They do not have access to the wordpress backend.

When I create a new post in the backend of wordpress, all media ends up in the folder “musterkuechen”.

However, when a User create a new post using the custom dashboard, the media goes into the standard uploads folder.
My script works in the wordpress backend dashboard, but not in the custom dashboard that I’ve made for my users.

What did I miss to make it work in the custom user dashboard?

Here is the Code:

add_filter('upload_dir', 'rrwd_upload_dir');

$upload = wp_upload_dir();
// remove_filter('upload_dir', 'rrwd_upload_dir');

function rrwd_upload_dir( $upload ) {

$id = $_REQUEST('post_id');
  $parent = get_post( $id )->post_parent;

  // Check the post-type of the current post
  if( "musterkuechen" == get_post_type( $id ) || "musterkuechen" == get_post_type( $parent ) )
    $upload('subdir') = '/musterkuechen' . $upload('subdir');

  $upload('path') = $upload('basedir') . $upload('subdir');
  $upload('url')  = $upload('baseurl') . $upload('subdir');

  return $upload;
}